From patchwork Fri Sep 28 18:29:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Murphy X-Patchwork-Id: 147807 Delivered-To: patch@linaro.org Received: by 2002:a2e:8595:0:0:0:0:0 with SMTP id b21-v6csp1083302lji; Fri, 28 Sep 2018 11:30:55 -0700 (PDT) X-Google-Smtp-Source: ACcGV60UywaSFPGNEihkkCoNkeOJ6MQKqgD4FbpzYBtN2D0dN4VH2nBrgdU6qFhWXTzi8D0oFQlM X-Received: by 2002:a17:902:c01:: with SMTP id 1-v6mr17120534pls.122.1538159455598; Fri, 28 Sep 2018 11:30:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538159455; cv=none; d=google.com; s=arc-20160816; b=FPYwUkaZha0sSCPdlwrVRJkUpqz/H/l95We2oIMvooAKs296QFhkLGhCzApTeuVKoO 2czgxvJUsiMejvmaQNPc1K3tpVgbZi1jXJoGudNBYjhoWr7yr81KW1nNOOA3bE0vV5DI oOp4/hgoSXWWEhYidH9sPYSz1P2ZLWT3+fLP1wrAXXyxlzCUtfznDwtpeINFGktpPgRA WgmIrTpUAaXTJ+YEgStxTCm1OfSIeVIqQ8+2edkAn+WvP+oSQQ9cF8CyMQvITsjRfLc3 h81tzTPRUIQ7uAI96dDFF3zzQzWXEOzdgdAyT89rGCMvr+ShCJisupBItNDSbWSJE8Xk 5zFQ== 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; bh=rVyNqgRXHB1JoKRGGWVEWYfuN9AHiW8WoanEkVBJmjw=; b=EOsjRzW5FfDt42y65RLSQxx0xAeu68LCxGBDaX8lnxJFmisDhqsdvb4RViiduQrmKm sjxTQ18tt0GGJvXXOl9oiqnL8riyGBQzq7XLvAcDVYM2FMAQ7UeQ1XWT648LvgAKOPqB GD/um5cw54CulNGOdlOufk6OY4+zg4s90AXeyqh4PHLUjF48W5IEvuxI9OovtezstS43 3I/sJWRZMuVo6mssl90DqMB5ifW5u+6jDo30HwUN2rCHVKKGNkOvLzq1GV0A9ydkNL9p Mh11G/osIwbtY8wsGoetK9Q3eLEmz51x3xWCbBdppJtSg2sjKLbdUSIDZTcII24Vpyu9 2J5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=DtP5Bomc; spf=pass (google.com: best guess record for domain of linux-omap-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-omap-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 g27-v6si6966889pfj.283.2018.09.28.11.30.55; Fri, 28 Sep 2018 11:30:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-omap-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=DtP5Bomc; spf=pass (google.com: best guess record for domain of linux-omap-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-omap-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 S1727433AbeI2Azz (ORCPT + 5 others); Fri, 28 Sep 2018 20:55:55 -0400 Received: from fllv0016.ext.ti.com ([198.47.19.142]:37626 "EHLO fllv0016.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726460AbeI2AzH (ORCPT ); Fri, 28 Sep 2018 20:55:07 -0400 Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by fllv0016.ext.ti.com (8.15.2/8.15.2) with ESMTP id w8SIU2OZ001973; Fri, 28 Sep 2018 13:30:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1538159402; bh=rVyNqgRXHB1JoKRGGWVEWYfuN9AHiW8WoanEkVBJmjw=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=DtP5Bomc3MZQ9BUJgFpfhGfsxRi2dQat7ChvTj6AICSpsuJFf/Xd2c7mDq358sb3J APz56Ux9PKkkKHYUZ15lrJCK78k1kraSdelJCjrJ8A4gyYOEYl9i1aBAwhle6HQpqU esIa9v+qNmWrCcRt76IFe7xRMwyBo/DPO1gJVs68= Received: from DFLE113.ent.ti.com (dfle113.ent.ti.com [10.64.6.34]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id w8SIU16a002152; Fri, 28 Sep 2018 13:30:02 -0500 Received: from DFLE105.ent.ti.com (10.64.6.26) by DFLE113.ent.ti.com (10.64.6.34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Fri, 28 Sep 2018 13:30:01 -0500 Received: from dflp32.itg.ti.com (10.64.6.15) 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; Fri, 28 Sep 2018 13:30:01 -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 w8SIU1I2002608; Fri, 28 Sep 2018 13:30:01 -0500 Received: from localhost (a0272616local-lt.dhcp.ti.com [172.22.149.181]) by legion.dal.design.ti.com (8.11.7p1+Sun/8.11.7) with ESMTP id w8SIU0x27714; Fri, 28 Sep 2018 13:30:00 -0500 (CDT) From: Dan Murphy To: , , CC: , , , , , Dan Murphy Subject: [RFC PATCH v2 5/9] leds: lm3697: Introduce the lm3697 driver Date: Fri, 28 Sep 2018 13:29:50 -0500 Message-ID: <20180928182954.25446-6-dmurphy@ti.com> X-Mailer: git-send-email 2.12.2 In-Reply-To: <20180928182954.25446-1-dmurphy@ti.com> References: <20180928182954.25446-1-dmurphy@ti.com> MIME-Version: 1.0 X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Sender: linux-omap-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-omap@vger.kernel.org Introduce the lm3697 LED driver for backlighting and display. Datasheet location: http://www.ti.com/lit/ds/symlink/lm3697.pdf Signed-off-by: Dan Murphy --- drivers/leds/Kconfig | 8 +- drivers/leds/Makefile | 1 + drivers/leds/leds-lm3697.c | 382 +++++++++++++++++++++++++++++++++++++ 3 files changed, 390 insertions(+), 1 deletion(-) create mode 100644 drivers/leds/leds-lm3697.c -- 2.19.0 diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index dc717b30d9d3..1de4cbb13bd2 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -756,9 +756,15 @@ config LEDS_NIC78BX To compile this driver as a module, choose M here: the module will be called leds-nic78bx. +config LEDS_LM3697 + tristate "LED driver for LM3697" + depends on LEDS_TI_LMU_COMMON + help + Say Y to enable the LED driver for TI LMU devices. + This supports the LED device LM3697. + config LEDS_TI_LMU_COMMON tristate "LED driver for TI LMU" - depends on REGMAP help Say Y to enable the LED driver for TI LMU devices. This supports common features between the TI LM3532, LM3631, LM3632, diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index e09bb27bc7ea..6fbce7cfc41c 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -78,6 +78,7 @@ obj-$(CONFIG_LEDS_MT6323) += leds-mt6323.o obj-$(CONFIG_LEDS_LM3692X) += leds-lm3692x.o obj-$(CONFIG_LEDS_SC27XX_BLTC) += leds-sc27xx-bltc.o obj-$(CONFIG_LEDS_LM3601X) += leds-lm3601x.o +obj-$(CONFIG_LEDS_LM3697) += leds-lm3697.o obj-$(CONFIG_LEDS_TI_LMU_COMMON) += ti-lmu-led-common.o # LED SPI Drivers diff --git a/drivers/leds/leds-lm3697.c b/drivers/leds/leds-lm3697.c new file mode 100644 index 000000000000..eac4073fe393 --- /dev/null +++ b/drivers/leds/leds-lm3697.c @@ -0,0 +1,382 @@ +// SPDX-License-Identifier: GPL-2.0 +// TI LM3697 LED chip family driver +// Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/ + +#include +#include +#include +#include + +#include "ti-lmu-led-common.h" + +#define LM3697_REV 0x0 +#define LM3697_RESET 0x1 +#define LM3697_OUTPUT_CONFIG 0x10 +#define LM3697_CTRL_A_RAMP 0x11 +#define LM3697_CTRL_B_RAMP 0x12 +#define LM3697_CTRL_A_B_RT_RAMP 0x13 +#define LM3697_CTRL_A_B_RAMP_CFG 0x14 +#define LM3697_CTRL_A_B_BRT_CFG 0x16 +#define LM3697_CTRL_A_FS_CURR_CFG 0x17 +#define LM3697_CTRL_B_FS_CURR_CFG 0x18 +#define LM3697_PWM_CFG 0x1c +#define LM3697_CTRL_A_BRT_LSB 0x20 +#define LM3697_CTRL_A_BRT_MSB 0x21 +#define LM3697_CTRL_B_BRT_LSB 0x22 +#define LM3697_CTRL_B_BRT_MSB 0x23 +#define LM3697_CTRL_ENABLE 0x24 + +#define LM3697_SW_RESET BIT(0) + +#define LM3697_CTRL_A_EN BIT(0) +#define LM3697_CTRL_B_EN BIT(1) +#define LM3697_CTRL_A_B_EN (LM3697_CTRL_A_EN | LM3697_CTRL_B_EN) + +#define LM3697_MAX_LED_STRINGS 3 + +#define LM3697_CONTROL_A 0 +#define LM3697_CONTROL_B 1 +#define LM3697_MAX_CONTROL_BANKS 2 + +#define LM3697_HVLED_ASSIGNMENT 1 + +/** + * struct lm3697_led - + * @hvled_strings: Array of LED strings associated with a control bank + * @label: LED label + * @led_dev: LED class device + * @priv: Pointer to the device struct + * @lmu_data: Register and setting values for common code + * @control_bank: Control bank the LED is associated to. 0 is control bank A + * 1 is control bank B + */ +struct lm3697_led { + u32 hvled_strings[LM3697_MAX_LED_STRINGS]; + char label[LED_MAX_NAME_SIZE]; + struct led_classdev led_dev; + struct lm3697 *priv; + struct ti_lmu_bank lmu_data; + int control_bank; +}; + +/** + * struct lm3697 - + * @enable_gpio: Hardware enable gpio + * @regulator: LED supply regulator pointer + * @client: Pointer to the I2C client + * @regmap: Devices register map + * @dev: Pointer to the devices device struct + * @lock: Lock for reading/writing the device + * @leds: Array of LED strings + */ +struct lm3697 { + struct gpio_desc *enable_gpio; + struct regulator *regulator; + struct i2c_client *client; + struct regmap *regmap; + struct device *dev; + struct mutex lock; + struct lm3697_led leds[]; +}; + +static const struct reg_default lm3697_reg_defs[] = { + {LM3697_OUTPUT_CONFIG, 0x6}, + {LM3697_CTRL_A_RAMP, 0x0}, + {LM3697_CTRL_B_RAMP, 0x0}, + {LM3697_CTRL_A_B_RT_RAMP, 0x0}, + {LM3697_CTRL_A_B_RAMP_CFG, 0x0}, + {LM3697_CTRL_A_B_BRT_CFG, 0x0}, + {LM3697_CTRL_A_FS_CURR_CFG, 0x13}, + {LM3697_CTRL_B_FS_CURR_CFG, 0x13}, + {LM3697_PWM_CFG, 0xc}, + {LM3697_CTRL_A_BRT_LSB, 0x0}, + {LM3697_CTRL_A_BRT_MSB, 0x0}, + {LM3697_CTRL_B_BRT_LSB, 0x0}, + {LM3697_CTRL_B_BRT_MSB, 0x0}, + {LM3697_CTRL_ENABLE, 0x0}, +}; + +static const struct regmap_config lm3697_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + + .max_register = LM3697_CTRL_ENABLE, + .reg_defaults = lm3697_reg_defs, + .num_reg_defaults = ARRAY_SIZE(lm3697_reg_defs), + .cache_type = REGCACHE_FLAT, +}; + +static int lm3697_brightness_set(struct led_classdev *led_cdev, + enum led_brightness brt_val) +{ + struct lm3697_led *led = container_of(led_cdev, struct lm3697_led, + led_dev); + int ctrl_en_val; + int ret; + + mutex_lock(&led->priv->lock); + + if (led->control_bank == LM3697_CONTROL_A) { + led->lmu_data.msb_brightness_reg = LM3697_CTRL_A_BRT_MSB; + led->lmu_data.lsb_brightness_reg = LM3697_CTRL_A_BRT_LSB; + ctrl_en_val = LM3697_CTRL_A_EN; + } else { + led->lmu_data.msb_brightness_reg = LM3697_CTRL_B_BRT_MSB; + led->lmu_data.lsb_brightness_reg = LM3697_CTRL_B_BRT_LSB; + ctrl_en_val = LM3697_CTRL_B_EN; + } + + if (brt_val == LED_OFF) + ret = regmap_update_bits(led->priv->regmap, LM3697_CTRL_ENABLE, + ctrl_en_val, ~ctrl_en_val); + else + ret = regmap_update_bits(led->priv->regmap, LM3697_CTRL_ENABLE, + ctrl_en_val, ctrl_en_val); + + ret = ti_lmu_common_set_brightness(&led->lmu_data, brt_val); + if (ret) + dev_err(&led->priv->client->dev, "Cannot write brightness\n"); + + mutex_unlock(&led->priv->lock); + return ret; +} + +static int lm3697_set_control_bank(struct lm3697 *priv) +{ + u8 control_bank_config = 0; + struct lm3697_led *led; + int ret, i; + + led = &priv->leds[0]; + if (led->control_bank == LM3697_CONTROL_A) + led = &priv->leds[1]; + + for (i = 0; i < LM3697_MAX_LED_STRINGS; i++) + if (led->hvled_strings[i] == LM3697_HVLED_ASSIGNMENT) + control_bank_config |= 1 << i; + + ret = regmap_write(priv->regmap, LM3697_OUTPUT_CONFIG, + control_bank_config); + if (ret) + dev_err(&priv->client->dev, "Cannot write OUTPUT config\n"); + + return ret; +} + +static int lm3697_init(struct lm3697 *priv) +{ + struct lm3697_led *led; + int i, ret; + + if (priv->enable_gpio) { + gpiod_direction_output(priv->enable_gpio, 1); + } else { + ret = regmap_write(priv->regmap, LM3697_RESET, LM3697_SW_RESET); + if (ret) { + dev_err(&priv->client->dev, "Cannot reset the device\n"); + goto out; + } + } + + ret = regmap_write(priv->regmap, LM3697_CTRL_ENABLE, 0x0); + if (ret) { + dev_err(&priv->client->dev, "Cannot write ctrl enable\n"); + goto out; + } + + ret = lm3697_set_control_bank(priv); + if (ret) + dev_err(&priv->client->dev, "Setting the CRTL bank failed\n"); + + for (i = 0; i < LM3697_MAX_CONTROL_BANKS; i++) { + led = &priv->leds[i]; + ti_lmu_common_set_ramp(&led->lmu_data); + if (ret) + dev_err(&priv->client->dev, "Setting the ramp rate failed\n"); + } +out: + return ret; +} + +static int lm3697_probe_dt(struct lm3697 *priv) +{ + struct fwnode_handle *child = NULL; + struct lm3697_led *led; + const char *name; + int control_bank; + size_t i = 0; + int ret; + + priv->enable_gpio = devm_gpiod_get_optional(&priv->client->dev, + "enable", GPIOD_OUT_LOW); + if (IS_ERR(priv->enable_gpio)) { + ret = PTR_ERR(priv->enable_gpio); + dev_err(&priv->client->dev, "Failed to get enable gpio: %d\n", + ret); + return ret; + } + + priv->regulator = devm_regulator_get(&priv->client->dev, "vled"); + if (IS_ERR(priv->regulator)) + priv->regulator = NULL; + + device_for_each_child_node(priv->dev, child) { + ret = fwnode_property_read_u32(child, "reg", &control_bank); + if (ret) { + dev_err(&priv->client->dev, "reg property missing\n"); + fwnode_handle_put(child); + goto child_out; + } + + if (control_bank > LM3697_CONTROL_B) { + dev_err(&priv->client->dev, "reg property is invalid\n"); + ret = -EINVAL; + fwnode_handle_put(child); + goto child_out; + } + + led = &priv->leds[i]; + + led->control_bank = control_bank; + led->lmu_data.bank_id = control_bank; + led->lmu_data.enable_reg = LM3697_CTRL_ENABLE; + led->lmu_data.regmap = priv->regmap; + if (control_bank == LM3697_CONTROL_A) + led->lmu_data.runtime_ramp_reg = LM3697_CTRL_A_RAMP; + else + led->lmu_data.runtime_ramp_reg = LM3697_CTRL_B_RAMP; + + ret = fwnode_property_read_u32_array(child, "led-sources", + led->hvled_strings, + LM3697_MAX_LED_STRINGS); + if (ret) { + dev_err(&priv->client->dev, "led-sources property missing\n"); + fwnode_handle_put(child); + goto child_out; + } + + ret = ti_lmu_common_get_ramp_params(&priv->client->dev, child, &led->lmu_data); + if (ret) + dev_warn(&priv->client->dev, "runtime-ramp properties missing\n"); + + fwnode_property_read_string(child, "linux,default-trigger", + &led->led_dev.default_trigger); + + ret = fwnode_property_read_string(child, "label", &name); + if (ret) + snprintf(led->label, sizeof(led->label), + "%s::", priv->client->name); + else + snprintf(led->label, sizeof(led->label), + "%s:%s", priv->client->name, name); + + led->priv = priv; + led->led_dev.name = led->label; + led->led_dev.brightness_set_blocking = lm3697_brightness_set; + + ret = devm_led_classdev_register(priv->dev, &led->led_dev); + if (ret) { + dev_err(&priv->client->dev, "led register err: %d\n", + ret); + fwnode_handle_put(child); + goto child_out; + } + + i++; + } + +child_out: + return ret; +} + +static int lm3697_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct lm3697 *led; + int count; + int ret; + + count = device_get_child_node_count(&client->dev); + if (!count) { + dev_err(&client->dev, "LEDs are not defined in device tree!"); + return -ENODEV; + } + + led = devm_kzalloc(&client->dev, struct_size(led, leds, count), + GFP_KERNEL); + if (!led) + return -ENOMEM; + + mutex_init(&led->lock); + i2c_set_clientdata(client, led); + + led->client = client; + led->dev = &client->dev; + led->regmap = devm_regmap_init_i2c(client, &lm3697_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 = lm3697_probe_dt(led); + if (ret) + return ret; + + return lm3697_init(led); +} + +static int lm3697_remove(struct i2c_client *client) +{ + struct lm3697 *led = i2c_get_clientdata(client); + int ret; + + ret = regmap_update_bits(led->regmap, LM3697_CTRL_ENABLE, + LM3697_CTRL_A_B_EN, 0); + if (ret) { + dev_err(&led->client->dev, "Failed to disable the device\n"); + return ret; + } + + 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"); + } + + mutex_destroy(&led->lock); + + return 0; +} + +static const struct i2c_device_id lm3697_id[] = { + { "lm3697", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, lm3697_id); + +static const struct of_device_id of_lm3697_leds_match[] = { + { .compatible = "ti,lm3697", }, + {}, +}; +MODULE_DEVICE_TABLE(of, of_lm3697_leds_match); + +static struct i2c_driver lm3697_driver = { + .driver = { + .name = "lm3697", + .of_match_table = of_lm3697_leds_match, + }, + .probe = lm3697_probe, + .remove = lm3697_remove, + .id_table = lm3697_id, +}; +module_i2c_driver(lm3697_driver); + +MODULE_DESCRIPTION("Texas Instruments LM3697 LED driver"); +MODULE_AUTHOR("Dan Murphy "); +MODULE_LICENSE("GPL v2"); From patchwork Fri Sep 28 18:29:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Murphy X-Patchwork-Id: 147805 Delivered-To: patch@linaro.org Received: by 2002:a2e:8595:0:0:0:0:0 with SMTP id b21-v6csp1082745lji; Fri, 28 Sep 2018 11:30:22 -0700 (PDT) X-Google-Smtp-Source: ACcGV60P21dojsj+PuMtP/Q+KMpRD5PL5dY7lGnzgtjnajdsVH1UTZ6sOTlxotACzJOx/nfA1W5W X-Received: by 2002:a17:902:d01:: with SMTP id 1-v6mr11762146plu.121.1538159422410; Fri, 28 Sep 2018 11:30:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538159422; cv=none; d=google.com; s=arc-20160816; b=FCzR9gjcqY48Aq3F3PYiivne5YJbAx7UJQVT8kswFm1aW0u+9G+dlGTSw9jvfG+6wL flVrNLs8CQSAQmBDQba1YHosWlta7zM7lNDahDP7aABYVHstRCIfZAHlwd6ovtKEw/ld gbXD6NAtcw5li7lmwjvQTHVWJdQ2oJYKJ20ResAJNbaHnVB/Om0jXUctveDrmBaecpiM e4LoIDcKZ3M57tv+IXD9JogFSkNvxNRNvMw4Zht3kCKxGAuOhdQMfjrl2rYt8N+Jl4VW CGYLgtZui02fqHC3wq04hOgOxNC+rIaxu3A3oceIbETSGAqCdy6UD89J6Fn8PzU/Iz7V t+Ig== 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; bh=DD8synU/q94qrP4THTaBw8VQXx8gz5MGohpOZ5nIlNM=; b=cvE1IugF4OqA8WM+Uknl/7Tvv/P7e81Aa3d0c2BsC0R+PpW0BujLhMiMUuFyIjRrcI +/uiYhnJuGF/rf5LAm/4csHN7Fme3wYzZStYl1BPxe3cIJszhdvCGLIRkpxb6RPw1k55 0UrsqQbeBHmTddwVRD46E7/h8F/1M/WDSGyMT0K5PwjYZouTFcLOJmY2Wf1clSH8k43x XL7Zf5Dselp1/0BNJgJtoQb965x/AFsW7o0h9NlAHSS906VJ8/4uTRx21qWIw59xNz0Z HK7N/TY+uZyYoFNxw2waIixfccAqc1FXwi0O+mPaNJLPK+o0lz9MbckR5crIn+X8mhPe p1cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=iwAtL0wn; spf=pass (google.com: best guess record for domain of linux-omap-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-omap-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 g7-v6si5259428plt.259.2018.09.28.11.30.22; Fri, 28 Sep 2018 11:30:22 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-omap-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=iwAtL0wn; spf=pass (google.com: best guess record for domain of linux-omap-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-omap-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 S1727358AbeI2AzL (ORCPT + 5 others); Fri, 28 Sep 2018 20:55:11 -0400 Received: from fllv0015.ext.ti.com ([198.47.19.141]:43140 "EHLO fllv0015.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727269AbeI2AzK (ORCPT ); Fri, 28 Sep 2018 20:55:10 -0400 Received: from dflxv15.itg.ti.com ([128.247.5.124]) by fllv0015.ext.ti.com (8.15.2/8.15.2) with ESMTP id w8SIU5LL085423; Fri, 28 Sep 2018 13:30:05 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1538159405; bh=DD8synU/q94qrP4THTaBw8VQXx8gz5MGohpOZ5nIlNM=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=iwAtL0wn41a0yGOWmI4g880EDt1RKF/6d1XL4lTwYZSk1SkG1oYNxYCo3OTOW29Nz VMUur6aY8Vr2jnbf4XfDJzvOV6aD5ATDh3XWeSqvAZjAof87PX9oSEjd6ilNE/H9rJ DXXJAT2DGhDPa45GhKONlcdJdXRkIN8sH5vJULsE= Received: from DLEE109.ent.ti.com (dlee109.ent.ti.com [157.170.170.41]) by dflxv15.itg.ti.com (8.14.3/8.13.8) with ESMTP id w8SIU4oK017734; Fri, 28 Sep 2018 13:30:04 -0500 Received: from DLEE103.ent.ti.com (157.170.170.33) by DLEE109.ent.ti.com (157.170.170.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Fri, 28 Sep 2018 13:30:03 -0500 Received: from dflp32.itg.ti.com (10.64.6.15) by DLEE103.ent.ti.com (157.170.170.33) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.1.1466.3 via Frontend Transport; Fri, 28 Sep 2018 13:30:03 -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 w8SIU3Cu003064; Fri, 28 Sep 2018 13:30:03 -0500 Received: from localhost (a0272616local-lt.dhcp.ti.com [172.22.149.181]) by legion.dal.design.ti.com (8.11.7p1+Sun/8.11.7) with ESMTP id w8SIU2x27738; Fri, 28 Sep 2018 13:30:02 -0500 (CDT) From: Dan Murphy To: , , CC: , , , , , Dan Murphy Subject: [RFC PATCH v2 7/9] leds: lm3633: Introduce the lm3633 driver Date: Fri, 28 Sep 2018 13:29:52 -0500 Message-ID: <20180928182954.25446-8-dmurphy@ti.com> X-Mailer: git-send-email 2.12.2 In-Reply-To: <20180928182954.25446-1-dmurphy@ti.com> References: <20180928182954.25446-1-dmurphy@ti.com> MIME-Version: 1.0 X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Sender: linux-omap-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-omap@vger.kernel.org Introduce the LED LM3633 driver. This LED driver has 9 total LED outputs with runtime internal ramp configurations. Data sheet: http://www.ti.com/lit/ds/symlink/lm3633.pdf Signed-off-by: Dan Murphy --- drivers/leds/Kconfig | 8 + drivers/leds/Makefile | 1 + drivers/leds/leds-lm3633.c | 557 +++++++++++++++++++++++++++++++++++++ 3 files changed, 566 insertions(+) create mode 100644 drivers/leds/leds-lm3633.c -- 2.19.0 diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index 1de4cbb13bd2..1a78588d9806 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -763,8 +763,16 @@ config LEDS_LM3697 Say Y to enable the LED driver for TI LMU devices. This supports the LED device LM3697. +config LEDS_LM3633 + tristate "LED driver for LM3633" + depends on LEDS_TI_LMU_COMMON + help + Say Y to enable the LED driver for TI LMU devices. + This supports the LED device LM3633. + config LEDS_TI_LMU_COMMON tristate "LED driver for TI LMU" + depends on REGMAP help Say Y to enable the LED driver for TI LMU devices. This supports common features between the TI LM3532, LM3631, LM3632, diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index 6fbce7cfc41c..6ec006892fc0 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -79,6 +79,7 @@ obj-$(CONFIG_LEDS_LM3692X) += leds-lm3692x.o obj-$(CONFIG_LEDS_SC27XX_BLTC) += leds-sc27xx-bltc.o obj-$(CONFIG_LEDS_LM3601X) += leds-lm3601x.o obj-$(CONFIG_LEDS_LM3697) += leds-lm3697.o +obj-$(CONFIG_LEDS_LM3633) += leds-lm3633.o obj-$(CONFIG_LEDS_TI_LMU_COMMON) += ti-lmu-led-common.o # LED SPI Drivers diff --git a/drivers/leds/leds-lm3633.c b/drivers/leds/leds-lm3633.c new file mode 100644 index 000000000000..bf946eed271a --- /dev/null +++ b/drivers/leds/leds-lm3633.c @@ -0,0 +1,557 @@ +// SPDX-License-Identifier: GPL-2.0 +// TI LM3633 LED chip family driver +// Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/ + +#include +#include +#include +#include + +#include "ti-lmu-led-common.h" + +#define LM3633_REV 0x0 +#define LM3633_RESET 0x1 +#define LM3633_HVLED_OUTPUT_CONFIG 0x10 +#define LM3633_LVLED_OUTPUT_CONFIG 0x11 + +#define LM3633_CTRL_A_RAMP 0x12 +#define LM3633_CTRL_B_RAMP 0x13 +#define LM3633_CTRL_C_RAMP 0x14 +#define LM3633_CTRL_D_RAMP 0x15 +#define LM3633_CTRL_E_RAMP 0x16 +#define LM3633_CTRL_F_RAMP 0x17 +#define LM3633_CTRL_G_RAMP 0x18 +#define LM3633_CTRL_H_RAMP 0x19 + +#define LM3633_CTRL_A_B_RT_RAMP 0x1a +#define LM3633_CTRL_A_B_RAMP_CFG 0x1b +#define LM3633_CTRL_C_E_RT_RAMP 0x1c +#define LM3633_CTRL_F_H_RT_RAMP 0x1d + +#define LM3633_CTRL_A_B_BRT_CFG 0x16 +#define LM3633_CTRL_A_FS_CURR_CFG 0x17 +#define LM3633_CTRL_B_FS_CURR_CFG 0x18 +#define LM3633_PWM_CFG 0x1c + +#define LM3633_CTRL_ENABLE 0x2b + +#define LM3633_CTRL_A_BRT_LSB 0x40 +#define LM3633_CTRL_A_BRT_MSB 0x41 +#define LM3633_CTRL_B_BRT_LSB 0x42 +#define LM3633_CTRL_B_BRT_MSB 0x43 +#define LM3633_CTRL_C_BRT 0x44 +#define LM3633_CTRL_D_BRT 0x45 +#define LM3633_CTRL_E_BRT 0x46 +#define LM3633_CTRL_F_BRT 0x47 +#define LM3633_CTRL_G_BRT 0x48 +#define LM3633_CTRL_H_BRT 0x49 + +#define LM3633_SW_RESET BIT(0) + +#define LM3633_CTRL_A_EN BIT(0) +#define LM3633_CTRL_B_EN BIT(1) +#define LM3633_CTRL_C_EN BIT(2) +#define LM3633_CTRL_D_EN BIT(3) +#define LM3633_CTRL_E_EN BIT(4) +#define LM3633_CTRL_F_EN BIT(5) +#define LM3633_CTRL_G_EN BIT(6) +#define LM3633_CTRL_H_EN BIT(7) + +#define LM3633_MAX_HVLED_STRINGS 3 +#define LM3633_MAX_LVLED_STRINGS 6 + +#define LM3633_CONTROL_A 0 +#define LM3633_CONTROL_B 1 +#define LM3633_CONTROL_C 2 +#define LM3633_CONTROL_D 3 +#define LM3633_CONTROL_E 4 +#define LM3633_CONTROL_F 5 +#define LM3633_CONTROL_G 6 +#define LM3633_CONTROL_H 7 + +#define LM3633_MAX_CONTROL_BANKS 8 + +#define LM3633_LED_ASSIGNMENT 1 + +#define LM3633_CTRL_F_EN_MASK 0x07 +#define LM3633_CTRL_EN_OFFSET 2 + +/** + * struct lm3633_led - + * @hvled_strings: Array of high voltage LED strings associated to control bank + * @lvled_strings: Array of low voltage LED strings associated to a control bank + * @label: LED label + * @led_dev: LED class device + * @priv: Pointer to the device struct + * @lmu_data: Register and setting values for common code + * @control_bank: Control bank the LED is associated to + */ +struct lm3633_led { + u32 hvled_strings[LM3633_MAX_HVLED_STRINGS]; + u32 lvled_strings[LM3633_MAX_LVLED_STRINGS]; + char label[LED_MAX_NAME_SIZE]; + struct led_classdev led_dev; + struct lm3633 *priv; + struct ti_lmu_bank lmu_data; + int control_bank; +}; + +/** + * struct lm3633 - + * @enable_gpio - Hardware enable gpio + * @regulator - LED supply regulator pointer + * @client - Pointer to the I2C client + * @regmap - Devices register map + * @dev - Pointer to the devices device struct + * @lock - Lock for reading/writing the device + * @leds - Array of LED strings + */ +struct lm3633 { + struct gpio_desc *enable_gpio; + struct regulator *regulator; + struct i2c_client *client; + struct regmap *regmap; + struct device *dev; + struct mutex lock; + struct lm3633_led leds[]; +}; + +static const struct reg_default lm3633_reg_defs[] = { + {LM3633_HVLED_OUTPUT_CONFIG, 0x6}, + {LM3633_LVLED_OUTPUT_CONFIG, 0x36}, + {LM3633_CTRL_A_RAMP, 0x0}, + {LM3633_CTRL_B_RAMP, 0x0}, + {LM3633_CTRL_A_B_RT_RAMP, 0x0}, + {LM3633_CTRL_A_B_RAMP_CFG, 0x0}, + {LM3633_CTRL_A_B_BRT_CFG, 0x0}, + {LM3633_CTRL_A_FS_CURR_CFG, 0x13}, + {LM3633_CTRL_B_FS_CURR_CFG, 0x13}, + {LM3633_PWM_CFG, 0xc}, + {LM3633_CTRL_A_BRT_LSB, 0x0}, + {LM3633_CTRL_A_BRT_MSB, 0x0}, + {LM3633_CTRL_B_BRT_LSB, 0x0}, + {LM3633_CTRL_B_BRT_MSB, 0x0}, + {LM3633_CTRL_ENABLE, 0x0}, +}; + +static const struct regmap_config lm3633_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + + .max_register = LM3633_CTRL_H_BRT, + .reg_defaults = lm3633_reg_defs, + .num_reg_defaults = ARRAY_SIZE(lm3633_reg_defs), + .cache_type = REGCACHE_FLAT, +}; + +static int lm3633_brightness_set(struct led_classdev *led_cdev, + enum led_brightness brt_val) +{ + struct lm3633_led *led = container_of(led_cdev, struct lm3633_led, + led_dev); + int ctrl_en_val; + int ret; + + mutex_lock(&led->priv->lock); + + switch (led->control_bank) { + case LM3633_CONTROL_A: + ctrl_en_val = LM3633_CTRL_A_EN; + break; + case LM3633_CONTROL_B: + ctrl_en_val = LM3633_CTRL_B_EN; + break; + case LM3633_CONTROL_C: + ctrl_en_val = LM3633_CTRL_C_EN; + break; + case LM3633_CONTROL_D: + ctrl_en_val = LM3633_CTRL_D_EN; + break; + case LM3633_CONTROL_E: + ctrl_en_val = LM3633_CTRL_E_EN; + break; + case LM3633_CONTROL_F: + ctrl_en_val = LM3633_CTRL_F_EN; + break; + case LM3633_CONTROL_G: + ctrl_en_val = LM3633_CTRL_G_EN; + break; + case LM3633_CONTROL_H: + ctrl_en_val = LM3633_CTRL_H_EN; + break; + default: + dev_err(&led->priv->client->dev, "Cannot write brightness\n"); + ret = -EINVAL; + goto out; + } + + if (brt_val == LED_OFF) + ret = regmap_update_bits(led->priv->regmap, LM3633_CTRL_ENABLE, + ctrl_en_val, ~ctrl_en_val); + else + ret = regmap_update_bits(led->priv->regmap, LM3633_CTRL_ENABLE, + ctrl_en_val, ctrl_en_val); + + ret = ti_lmu_common_set_brightness(&led->lmu_data, brt_val); + if (ret) + dev_err(&led->priv->client->dev, "Cannot write brightness\n"); +out: + mutex_unlock(&led->priv->lock); + return ret; +} + +static void lm3633_set_control_bank_regs(struct lm3633_led *led) +{ + switch (led->control_bank) { + case LM3633_CONTROL_A: + led->lmu_data.msb_brightness_reg = LM3633_CTRL_A_BRT_MSB; + led->lmu_data.lsb_brightness_reg = LM3633_CTRL_A_BRT_LSB; + led->lmu_data.runtime_ramp_reg = LM3633_CTRL_A_RAMP; + case LM3633_CONTROL_B: + led->lmu_data.msb_brightness_reg = LM3633_CTRL_B_BRT_MSB; + led->lmu_data.lsb_brightness_reg = LM3633_CTRL_B_BRT_LSB; + led->lmu_data.runtime_ramp_reg = LM3633_CTRL_B_RAMP; + break; + case LM3633_CONTROL_C: + led->lmu_data.msb_brightness_reg = LM3633_CTRL_C_BRT; + led->lmu_data.runtime_ramp_reg = LM3633_CTRL_C_E_RT_RAMP; + break; + case LM3633_CONTROL_D: + led->lmu_data.msb_brightness_reg = LM3633_CTRL_D_BRT; + led->lmu_data.runtime_ramp_reg = LM3633_CTRL_C_E_RT_RAMP; + break; + case LM3633_CONTROL_E: + led->lmu_data.msb_brightness_reg = LM3633_CTRL_E_BRT; + led->lmu_data.runtime_ramp_reg = LM3633_CTRL_C_E_RT_RAMP; + break; + case LM3633_CONTROL_F: + led->lmu_data.msb_brightness_reg = LM3633_CTRL_F_BRT; + led->lmu_data.runtime_ramp_reg = LM3633_CTRL_F_H_RT_RAMP; + break; + case LM3633_CONTROL_G: + led->lmu_data.msb_brightness_reg = LM3633_CTRL_G_BRT; + led->lmu_data.runtime_ramp_reg = LM3633_CTRL_F_H_RT_RAMP; + break; + case LM3633_CONTROL_H: + led->lmu_data.msb_brightness_reg = LM3633_CTRL_H_BRT; + led->lmu_data.runtime_ramp_reg = LM3633_CTRL_F_H_RT_RAMP; + break; + default: + dev_err(&led->priv->client->dev, + "Control bank is out of bounds\n"); + } +} + +static int lm3633_set_control_bank(struct lm3633 *priv) +{ + u8 control_bank_config = 0; + struct lm3633_led *led; + int ret, i; + + led = &priv->leds[0]; + if (led->control_bank == LM3633_CONTROL_A) { + lm3633_set_control_bank_regs(led); + led = &priv->leds[1]; + } + + if (led->control_bank >= LM3633_CONTROL_C) + return 0; + + lm3633_set_control_bank_regs(led); + for (i = 0; i < LM3633_MAX_HVLED_STRINGS; i++) + if (led->hvled_strings[i] == LM3633_LED_ASSIGNMENT) + control_bank_config |= 1 << i; + + ret = regmap_write(priv->regmap, LM3633_HVLED_OUTPUT_CONFIG, + control_bank_config); + if (ret) + dev_err(&priv->client->dev, "Cannot write OUTPUT config\n"); + + return ret; +} + +static int lm3633_set_lvled_control_bank(struct lm3633 *priv) +{ + u8 control_bank_config = 0; + struct lm3633_led *led; + int ret, i; + + for (i = 0; i <= LM3633_MAX_CONTROL_BANKS; i++) { + led = &priv->leds[i]; + if (led) { + if (led->control_bank < LM3633_CONTROL_C) + continue; + + if (led->lvled_strings[0]) { + if (led->control_bank == LM3633_CONTROL_C) + control_bank_config = 0x0; + else if (led->control_bank == LM3633_CONTROL_F) + control_bank_config &= LM3633_CTRL_F_EN_MASK; + else + control_bank_config |= 1 << (led->control_bank - LM3633_CTRL_EN_OFFSET); + } + } else + continue; + + lm3633_set_control_bank_regs(led); + } + + ret = regmap_write(priv->regmap, LM3633_LVLED_OUTPUT_CONFIG, + control_bank_config); + if (ret) + dev_err(&priv->client->dev, "Cannot write OUTPUT config\n"); + + return ret; +} + +static int lm3633_init(struct lm3633 *priv) +{ + struct lm3633_led *led; + int i, ret; + + if (priv->enable_gpio) { + gpiod_direction_output(priv->enable_gpio, 1); + } else { + ret = regmap_write(priv->regmap, LM3633_RESET, LM3633_SW_RESET); + if (ret) { + dev_err(&priv->client->dev, + "Cannot reset the device\n"); + goto out; + } + } + + ret = regmap_write(priv->regmap, LM3633_CTRL_ENABLE, 0x0); + if (ret) { + dev_err(&priv->client->dev, "Cannot write ctrl enable\n"); + goto out; + } + + ret = lm3633_set_control_bank(priv); + if (ret) + dev_err(&priv->client->dev, "Setting the CRTL bank failed\n"); + + ret = lm3633_set_lvled_control_bank(priv); + if (ret) + dev_err(&priv->client->dev, + "Setting the lvled CRTL bank failed\n"); + + for (i = 0; i < LM3633_MAX_CONTROL_BANKS; i++) { + led = &priv->leds[i]; + if (led->lmu_data.runtime_ramp_reg) { + ti_lmu_common_set_ramp(&led->lmu_data); + if (ret) + dev_err(&priv->client->dev, + "Setting the ramp rate failed\n"); + } + } +out: + return ret; +} + +static int lm3633_parse_hvled_sources(struct fwnode_handle *child, + struct lm3633_led *led) +{ + struct lm3633 *priv = led->priv; + int ret; + + ret = fwnode_property_read_u32_array(child, "led-sources", + led->hvled_strings, + LM3633_MAX_HVLED_STRINGS); + + if (ret) + dev_err(&priv->client->dev, "Cannot write OUTPUT config\n"); + + return ret; +} + +static int lm3633_parse_lvled_sources(struct fwnode_handle *child, + struct lm3633_led *led) +{ + struct lm3633 *priv = led->priv; + int ret; + + ret = fwnode_property_read_u32_array(child, "led-sources", + led->lvled_strings, 1); + if (ret) + dev_err(&priv->client->dev, "Cannot write OUTPUT config\n"); + + led->lmu_data.max_brightness = MAX_BRIGHTNESS_8BIT; + + return 0; +} + +static int lm3633_probe_dt(struct lm3633 *priv) +{ + struct fwnode_handle *child = NULL; + struct lm3633_led *led; + const char *name; + int control_bank; + size_t i = 0; + int ret; + + priv->enable_gpio = devm_gpiod_get_optional(&priv->client->dev, + "enable", GPIOD_OUT_LOW); + if (IS_ERR(priv->enable_gpio)) { + ret = PTR_ERR(priv->enable_gpio); + dev_err(&priv->client->dev, "Failed to get enable gpio: %d\n", + ret); + return ret; + } + + priv->regulator = devm_regulator_get(&priv->client->dev, "vled"); + if (IS_ERR(priv->regulator)) + priv->regulator = NULL; + + device_for_each_child_node(priv->dev, child) { + ret = fwnode_property_read_u32(child, "reg", &control_bank); + if (ret) { + dev_err(&priv->client->dev, "reg property missing\n"); + fwnode_handle_put(child); + goto child_out; + } + + if (control_bank > LM3633_MAX_CONTROL_BANKS) { + dev_err(&priv->client->dev, + "reg property is invalid\n"); + ret = -EINVAL; + fwnode_handle_put(child); + goto child_out; + } + + led = &priv->leds[i]; + led->control_bank = control_bank; + led->lmu_data.bank_id = control_bank; + led->lmu_data.regmap = priv->regmap; + led->lmu_data.enable_reg = LM3633_CTRL_ENABLE; + + if (control_bank > LM3633_CONTROL_B) + lm3633_parse_lvled_sources(child, led); + else + lm3633_parse_hvled_sources(child, led); + + ret = ti_lmu_common_get_ramp_params(&priv->client->dev, + child, &led->lmu_data); + if (ret) + dev_warn(&priv->client->dev, + "runtime-ramp properties missing\n"); + + fwnode_property_read_string(child, "linux,default-trigger", + &led->led_dev.default_trigger); + + ret = fwnode_property_read_string(child, "label", &name); + if (ret) + snprintf(led->label, sizeof(led->label), + "%s::", priv->client->name); + else + snprintf(led->label, sizeof(led->label), + "%s:%s", priv->client->name, name); + + led->priv = priv; + led->led_dev.name = led->label; + led->led_dev.brightness_set_blocking = lm3633_brightness_set; + + ret = devm_led_classdev_register(priv->dev, &led->led_dev); + if (ret) { + dev_err(&priv->client->dev, "led register err: %d\n", + ret); + fwnode_handle_put(child); + goto child_out; + } + + i++; + } + +child_out: + return ret; +} + +static int lm3633_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct lm3633 *led; + int count; + int ret; + + count = device_get_child_node_count(&client->dev); + if (!count) { + dev_err(&client->dev, "LEDs are not defined in device tree!"); + return -ENODEV; + } + + led = devm_kzalloc(&client->dev, struct_size(led, leds, count), + GFP_KERNEL); + if (!led) + return -ENOMEM; + + mutex_init(&led->lock); + i2c_set_clientdata(client, led); + + led->client = client; + led->dev = &client->dev; + led->regmap = devm_regmap_init_i2c(client, &lm3633_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 = lm3633_probe_dt(led); + if (ret) + return ret; + + return lm3633_init(led); +} + +static int lm3633_remove(struct i2c_client *client) +{ + struct lm3633 *led = i2c_get_clientdata(client); + int ret; + + ret = regmap_write(led->regmap, LM3633_CTRL_ENABLE, 0); + if (ret) { + dev_err(&led->client->dev, "Failed to disable the device\n"); + return ret; + } + + 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"); + } + + mutex_destroy(&led->lock); + + return 0; +} + +static const struct i2c_device_id lm3633_id[] = { + { "lm3633", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, lm3633_id); + +static const struct of_device_id of_lm3633_leds_match[] = { + { .compatible = "ti,lm3633", }, + {}, +}; +MODULE_DEVICE_TABLE(of, of_lm3633_leds_match); + +static struct i2c_driver lm3633_driver = { + .driver = { + .name = "lm3633", + .of_match_table = of_lm3633_leds_match, + }, + .probe = lm3633_probe, + .remove = lm3633_remove, + .id_table = lm3633_id, +}; +module_i2c_driver(lm3633_driver); + +MODULE_DESCRIPTION("Texas Instruments LM3633 LED driver"); +MODULE_AUTHOR("Dan Murphy "); +MODULE_LICENSE("GPL v2"); From patchwork Fri Sep 28 18:29:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Murphy X-Patchwork-Id: 147804 Delivered-To: patch@linaro.org Received: by 2002:a2e:8595:0:0:0:0:0 with SMTP id b21-v6csp1082666lji; Fri, 28 Sep 2018 11:30:18 -0700 (PDT) X-Google-Smtp-Source: ACcGV624sJkqPZj68yQeYN1ItB+EAPCHpDtS9nY0asACFiLV6mLDinavjZIEO/l7PdLF25Uv4sbj X-Received: by 2002:a63:eb53:: with SMTP id b19-v6mr16147214pgk.371.1538159418440; Fri, 28 Sep 2018 11:30:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538159418; cv=none; d=google.com; s=arc-20160816; b=avtfqXb5LMAYTxRMgfc2Efl4e0vichwxY0N0gHvUPXC1GjWq0Ij2Y86dVZh/V3037m SxR9BrGjSuQCHChRnSR0OQ7hklCFMaDAp6wBsQXEBgFaeHnQksTngXJqvreq9Wk1R3sT YYlfi8ENNuldAbPWJ6UPRJkPxgT8rJqkD1ChPO4HVMigLtSgafJuEzjCXCoOllpIAZqd 6CWESSsuLQ2DxZgo46eLE7c9N5bOG4jVUbTlmZNrcP/zlqft9Y9vNcu4RDomKUCViyIh hIZsy8ueFyfLDFl92vGWMa+0smQPsnYWRbeCk+cOOunNfVI3hBEIcCaNdBmeczURufCh 3CWA== 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; bh=tugpfxs7hWuLGIhBoHwODyOE8X1lSeHDnSXf/S2tjqI=; b=tma4T6+vYc3s4wmgCxGdX6XCDs0+Hobyjg/mgba1Rb/cSk+bc9yS7IjAyyRTOMteh+ zMfIVSb6dgaz7xXBB/ZUds3gI5jRtxKR0qFdBajoOBpdfuIH794VkKO/FJ7bt+trTpfR pPsfbwp7EqK+AAzJh3H39SipNJ6j3hxOfAHJFjdGakHluyp3jHpXzTs+DH77bziohXtB ySv+DQjL8hUY4krAeH+cq+o0YXu039SefPBUa+zLRn6ioUYH2e+3lLLLEO7A9FX6jmYc aNDbKAw6CE0Y8ipy5NWu/defpdU1oi04IlE8ClWSE8YVa9iL3ZJ+c37WVWFT0Vle5fKK c+AA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=YtIp3CyJ; spf=pass (google.com: best guess record for domain of linux-omap-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-omap-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 r39-v6si5352963pld.218.2018.09.28.11.30.18; Fri, 28 Sep 2018 11:30:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-omap-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=YtIp3CyJ; spf=pass (google.com: best guess record for domain of linux-omap-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-omap-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 S1727385AbeI2AzL (ORCPT + 5 others); Fri, 28 Sep 2018 20:55:11 -0400 Received: from fllv0015.ext.ti.com ([198.47.19.141]:43138 "EHLO fllv0015.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726451AbeI2AzK (ORCPT ); Fri, 28 Sep 2018 20:55:10 -0400 Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by fllv0015.ext.ti.com (8.15.2/8.15.2) with ESMTP id w8SIU4n3085418; Fri, 28 Sep 2018 13:30:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1538159404; bh=tugpfxs7hWuLGIhBoHwODyOE8X1lSeHDnSXf/S2tjqI=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=YtIp3CyJqELen65QTpvOi9NKyoSFLb5u3CUbvkj1xjMWTEFIt62QowuhXyWb2eQTM q0qrTuDyj/X6OjOjWDlya3NGjffKrIwdvv2RguTnBx9SZKezVfMSpWNjc4tmL8uDG1 HpBJWWnyCzIOJwtWvGmw7mzadEx73wfEXizNxqJU= Received: from DFLE109.ent.ti.com (dfle109.ent.ti.com [10.64.6.30]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id w8SIU4fm002895; Fri, 28 Sep 2018 13:30:04 -0500 Received: from DFLE107.ent.ti.com (10.64.6.28) by DFLE109.ent.ti.com (10.64.6.30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Fri, 28 Sep 2018 13:30:04 -0500 Received: from dflp33.itg.ti.com (10.64.6.16) 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.1466.3 via Frontend Transport; Fri, 28 Sep 2018 13:30:04 -0500 Received: from legion.dal.design.ti.com (legion.dal.design.ti.com [128.247.22.53]) by dflp33.itg.ti.com (8.14.3/8.13.8) with ESMTP id w8SIU4Q6015745; Fri, 28 Sep 2018 13:30:04 -0500 Received: from localhost (a0272616local-lt.dhcp.ti.com [172.22.149.181]) by legion.dal.design.ti.com (8.11.7p1+Sun/8.11.7) with ESMTP id w8SIU3x27747; Fri, 28 Sep 2018 13:30:03 -0500 (CDT) From: Dan Murphy To: , , CC: , , , , , Dan Murphy Subject: [RFC PATCH v2 9/9] leds: lm3632: Introduce the TI LM3632 driver Date: Fri, 28 Sep 2018 13:29:54 -0500 Message-ID: <20180928182954.25446-10-dmurphy@ti.com> X-Mailer: git-send-email 2.12.2 In-Reply-To: <20180928182954.25446-1-dmurphy@ti.com> References: <20180928182954.25446-1-dmurphy@ti.com> MIME-Version: 1.0 X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Sender: linux-omap-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-omap@vger.kernel.org Add the dedicated TI LM3632 LED driver. This LED device is capable of driving a backlight display. In addition to the backlight the device has control of a strobe and torch output. Data sheet: http://www.ti.com/lit/ds/symlink/lm3632a.pdf Signed-off-by: Dan Murphy --- drivers/leds/Kconfig | 10 +- drivers/leds/Makefile | 1 + drivers/leds/leds-lm3632.c | 646 +++++++++++++++++++++++++++++++++++++ 3 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 drivers/leds/leds-lm3632.c -- 2.19.0 diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index 1a78588d9806..6174ab806cfb 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -764,8 +764,16 @@ config LEDS_LM3697 This supports the LED device LM3697. config LEDS_LM3633 - tristate "LED driver for LM3633" + tristate "LED driver for LM3632" depends on LEDS_TI_LMU_COMMON + help + Say Y to enable the LED driver for TI LMU devices. + This supports the LED device LM3632. + +config LEDS_LM3632 + tristate "LED driver for LM3632" + depends on LEDS_TI_LMU_COMMON + depends on LEDS_CLASS_FLASH help Say Y to enable the LED driver for TI LMU devices. This supports the LED device LM3633. diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index 6ec006892fc0..53d02d7f1a0b 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -79,6 +79,7 @@ obj-$(CONFIG_LEDS_LM3692X) += leds-lm3692x.o obj-$(CONFIG_LEDS_SC27XX_BLTC) += leds-sc27xx-bltc.o obj-$(CONFIG_LEDS_LM3601X) += leds-lm3601x.o obj-$(CONFIG_LEDS_LM3697) += leds-lm3697.o +obj-$(CONFIG_LEDS_LM3632) += leds-lm3632.o obj-$(CONFIG_LEDS_LM3633) += leds-lm3633.o obj-$(CONFIG_LEDS_TI_LMU_COMMON) += ti-lmu-led-common.o diff --git a/drivers/leds/leds-lm3632.c b/drivers/leds/leds-lm3632.c new file mode 100644 index 000000000000..ac3c21180444 --- /dev/null +++ b/drivers/leds/leds-lm3632.c @@ -0,0 +1,646 @@ +// SPDX-License-Identifier: GPL-2.0 +// Flash and torch driver for Texas Instruments LM3632 LED +// Flash driver chip family +// Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/ + +#include + +#include "ti-lmu-led-common.h" + +#define LM3632_MODE_BL 0x0 +#define LM3632_MODE_TORCH 0x1 + +/* Registers */ +#define LM3632_REV_REG 0x01 +#define LM3632_CFG1_REG 0x02 +#define LM3632_CFG2_REG 0x03 +#define LM3633_BL_BRT_LSB 0x04 +#define LM3633_BL_BRT_MSB 0x05 +#define LM3632_FLASH_TORCH_BRT 0x06 +#define LM3632_FLASH_CFG 0x07 +#define LM3632_IO_CTRL 0x09 +#define LM3632_ENABLE_REG 0x0a +#define LM3632_FLAGS1_REG 0x0b +#define LM3632_FLAGS2_REG 0x10 + +/* Enable Mode bits */ +#define LM3632_BL_EN BIT(0) +#define LM3632_FLASH_OUT_EN BIT(1) +#define LM3632_FLASH_MODE BIT(2) +#define LM3632_BLED1_2_EN BIT(3) +#define LM3632_BLED1_EN BIT(4) +#define LM3632_BLED1_2_MASK (LM3632_BL_EN | LM3632_BLED1_2_EN | LM3632_BLED1_EN) +#define LM3632_BL_OVP_EN BIT(6) +#define LM3632_SW_RESET BIT(7) + +/* Flags 1 Mask */ +#define LM3632_THERM_SHUTDOWN BIT(0) +#define LM3632_FLASH_TIME_OUT BIT(1) +#define LM3632_FLED_SHORT_FAULT BIT(2) +#define LM3632_VINM_SHORT_FAULT BIT(4) +#define LM3632_FOUT_SHORT_FAULT BIT(5) +#define LM3632_FLASH_OVP_FAULT BIT(6) +#define LM3632_BL_OVP_FAULT BIT(7) + +/* Flags 2 Mask */ +#define LM3632_BL_OCP_FAULT BIT(0) +#define LM3632_FLASH_OCP_FAULT BIT(1) +#define LM3632_VNEG_SHORT_FAULT BIT(2) +#define LM3632_VPOS_SHORT_FAULT BIT(3) +#define LM3632_VNEG_OVP_FAULT BIT(4) +#define LM3632_LCM_OVP_FAULT BIT(5) + +/* IO CTRL bits */ +#define LM3632_VINM_EN BIT(0) +#define LM3632_VINM_MODE_EN BIT(1) +#define LM3632_TX_EN BIT(2) +#define LM3632_HW_STROBE_EN BIT(4) +#define LM3632_PWM_EN BIT(6) + +#define LM3632_TORCH_BRT_SHIFT 4 + +#define LM3632_MAX_TORCH_I_UA 375000 +#define LM3632_MIN_TORCH_I_UA 25000 +#define LM3632_TORCH_STEP_UA 25000 + +#define LM3632_MAX_STROBE_I_UA 1500000 +#define LM3632_MIN_STROBE_I_UA 100000 +#define LM3632_STROBE_STEP_UA 100000 + +#define LM3632_TIMEOUT_MASK 0x1f +#define LM3632_ENABLE_MASK (LM3632_BL_EN | LM3632_FLASH_OUT_EN) + +#define LM3632_TIMEOUT_STEP_US 32000 +#define LM3632_MIN_TIMEOUT_US 32000 +#define LM3632_MAX_TIMEOUT_US 1024000 + +#define LM3632_TORCH_BRT_MASK 0xf0 +#define LM3632_FLASH_BRT_MASK 0xf + +#define LM3632_NUM_OF_BL_STRINGS 2 +#define LM3632_BL_ENABLED 1 +#define LM3632_BL1_ENABLE_SRC 0 +#define LM3632_BL12_ENABLE_SRC 1 + +/** + * struct lm3632_led - + * @fled_cdev: flash LED class device pointer + * @led_name: LED label + * @led_dev: LED class device + * @priv: Pointer to the lm3632 struct + * @lmu_data: Register and setting values for common code + * @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 + */ +struct lm3632_led { + struct led_classdev_flash fled_cdev; + u32 led_strings[LM3632_NUM_OF_BL_STRINGS]; + char led_name[LED_MAX_NAME_SIZE]; + struct led_classdev led_dev; + struct ti_lmu_bank lmu_data; + struct lm3632 *priv; + + unsigned int flash_timeout; + unsigned int last_flag; + + u32 torch_current_max; + u32 flash_current_max; + u32 max_flash_timeout; +}; + +/** + * struct lm3632 - + * @client: Pointer to the I2C client + * @regmap: Devices register map + * @lock: Lock for reading/writing the device + * @strobe_enable_gpio: Indicates whether strobe is HW controlled + * @leds: Array of LED strings + */ +struct lm3632 { + struct i2c_client *client; + struct regmap *regmap; + struct device *dev; + struct mutex lock; + int strobe_enable_gpio; + + struct lm3632_led leds[]; +}; + +static const struct reg_default lm3632_regmap_defs[] = { + { LM3632_CFG1_REG, 0x30 }, + { LM3632_CFG2_REG, 0x0d }, + { LM3632_FLASH_CFG, 0x2f }, + { LM3632_ENABLE_REG, 0x00 }, +}; + +static bool lm3632_volatile_reg(struct device *dev, unsigned int reg) +{ + switch (reg) { + case LM3632_FLAGS1_REG: + case LM3632_FLAGS2_REG: + return true; + default: + return false; + } +} + +static const struct regmap_config lm3632_regmap = { + .reg_bits = 8, + .val_bits = 8, + + .max_register = LM3632_FLAGS2_REG, + .reg_defaults = lm3632_regmap_defs, + .num_reg_defaults = ARRAY_SIZE(lm3632_regmap_defs), + .cache_type = REGCACHE_RBTREE, + .volatile_reg = lm3632_volatile_reg, +}; + +static struct lm3632_led *fled_cdev_to_led(struct led_classdev_flash *fled_cdev) +{ + return container_of(fled_cdev, struct lm3632_led, fled_cdev); +} + +static int lm3632_read_faults(struct lm3632_led *led) +{ + struct lm3632 *priv = led->priv; + int flags_val; + int ret; + + ret = regmap_read(priv->regmap, LM3632_FLAGS1_REG, &flags_val); + if (ret < 0) + return -EIO; + + led->last_flag = 0; + + if (flags_val & LM3632_FLASH_OVP_FAULT) + led->last_flag |= LED_FAULT_OVER_VOLTAGE; + + if (flags_val & LM3632_THERM_SHUTDOWN) + led->last_flag |= LED_FAULT_OVER_TEMPERATURE; + + if (flags_val & (LM3632_FLED_SHORT_FAULT | LM3632_VINM_SHORT_FAULT | + LM3632_FOUT_SHORT_FAULT)) + led->last_flag |= LED_FAULT_SHORT_CIRCUIT; + + if (flags_val & LM3632_THERM_SHUTDOWN) + led->last_flag |= LED_FAULT_LED_OVER_TEMPERATURE; + + if (flags_val & LM3632_FLASH_TIME_OUT) + led->last_flag |= LED_FAULT_TIMEOUT; + + ret = regmap_read(priv->regmap, LM3632_FLAGS2_REG, &flags_val); + if (ret < 0) + return -EIO; + + if (flags_val & (LM3632_BL_OCP_FAULT | LM3632_FLASH_OCP_FAULT)) + led->last_flag |= LED_FAULT_OVER_CURRENT; + + if (flags_val & LM3632_FLASH_OCP_FAULT) + led->last_flag |= LED_FAULT_OVER_CURRENT; + + return led->last_flag; +} + +static int lm3632_backlight_brightness_set(struct led_classdev *cdev, + enum led_brightness brightness) +{ + struct lm3632_led *led = container_of(cdev, struct lm3632_led, led_dev); + struct lm3632 *priv = led->priv; + int ret; + + mutex_lock(&priv->lock); + + ret = ti_lmu_common_set_brightness(&led->lmu_data, brightness); + if (ret) + dev_err(&priv->client->dev, "Cannot write brightness\n"); + + mutex_unlock(&priv->lock); + return ret; +} + +static int lm3632_torch_brightness_set(struct led_classdev *cdev, + enum led_brightness brightness) +{ + struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(cdev); + struct lm3632_led *led = fled_cdev_to_led(fled_cdev); + struct lm3632 *priv = led->priv; + int ret, brightness_val; + unsigned int reg_val; + + mutex_lock(&priv->lock); + + ret = lm3632_read_faults(led); + if (ret < 0) + goto out; + + if (brightness == LED_OFF) + ret = regmap_update_bits(priv->regmap, LM3632_ENABLE_REG, + LM3632_FLASH_OUT_EN, + ~LM3632_FLASH_OUT_EN); + else { + regmap_read(priv->regmap, LM3632_FLASH_TORCH_BRT, ®_val); + brightness_val = (brightness - LM3632_TORCH_STEP_UA) / LM3632_TORCH_STEP_UA; + brightness_val |= (reg_val & LM3632_FLASH_BRT_MASK); + + ret = regmap_write(priv->regmap, LM3632_FLASH_TORCH_BRT, + brightness_val << LM3632_TORCH_BRT_SHIFT); + if (ret) { + dev_err(&priv->client->dev, "Cannot write brightness\n"); + goto out; + } + + ret = regmap_update_bits(priv->regmap, LM3632_ENABLE_REG, + LM3632_FLASH_MODE | LM3632_FLASH_OUT_EN, + LM3632_FLASH_OUT_EN | ~LM3632_FLASH_MODE); + if (ret) + dev_err(&priv->client->dev, "Cannot write enable\n"); + } +out: + mutex_unlock(&priv->lock); + return ret; +} + +static int lm3632_strobe_set(struct led_classdev_flash *fled_cdev, bool state) +{ + struct lm3632_led *led = fled_cdev_to_led(fled_cdev); + struct lm3632 *priv = led->priv; + int timeout_reg_val; + int current_timeout; + int ret; + + mutex_lock(&priv->lock); + + ret = regmap_read(priv->regmap, LM3632_FLASH_CFG, ¤t_timeout); + if (ret < 0) + goto out; + + if (led->flash_timeout != current_timeout) { + timeout_reg_val = (led->flash_timeout - LM3632_TIMEOUT_STEP_US) / LM3632_TIMEOUT_STEP_US; + ret = regmap_update_bits(priv->regmap, LM3632_FLASH_CFG, + LM3632_TIMEOUT_MASK, timeout_reg_val); + if (ret) { + dev_err(&priv->client->dev, "Cannot write timeout\n"); + goto out; + } + } + + if (state && !priv->strobe_enable_gpio) { + ret = regmap_update_bits(priv->regmap, LM3632_ENABLE_REG, + LM3632_FLASH_MODE | LM3632_FLASH_OUT_EN, + LM3632_FLASH_OUT_EN | LM3632_FLASH_MODE); + if (ret) { + dev_err(&priv->client->dev, "Cannot write flash en\n"); + goto out; + } + } + + ret = lm3632_read_faults(led); +out: + mutex_unlock(&priv->lock); + return ret; +} + +static int lm3632_flash_brightness_set(struct led_classdev_flash *fled_cdev, + u32 brightness) +{ + struct lm3632_led *led = fled_cdev_to_led(fled_cdev); + struct lm3632 *priv = led->priv; + u32 brightness_val; + int ret; + unsigned int reg_val; + + mutex_lock(&priv->lock); + ret = lm3632_read_faults(led); + if (ret < 0) + goto out; + + brightness_val = (brightness - LM3632_STROBE_STEP_UA) / LM3632_STROBE_STEP_UA; + regmap_read(priv->regmap, LM3632_FLASH_TORCH_BRT, ®_val); + + brightness_val |= (reg_val & LM3632_TORCH_BRT_MASK); + ret = regmap_write(priv->regmap, LM3632_FLASH_TORCH_BRT, brightness_val); + if (ret) + dev_err(&priv->client->dev, "Cannot write brightness\n"); + +out: + mutex_unlock(&priv->lock); + return ret; +} + +static int lm3632_flash_timeout_set(struct led_classdev_flash *fled_cdev, + u32 timeout) +{ + struct lm3632_led *led = fled_cdev_to_led(fled_cdev); + struct lm3632 *priv = led->priv; + + mutex_lock(&priv->lock); + + led->flash_timeout = timeout; + + mutex_unlock(&priv->lock); + + return 0; +} + +static int lm3632_strobe_get(struct led_classdev_flash *fled_cdev, bool *state) +{ + struct lm3632_led *led = fled_cdev_to_led(fled_cdev); + struct lm3632 *priv = led->priv; + int strobe_state; + int ret; + + mutex_lock(&priv->lock); + + ret = regmap_read(priv->regmap, LM3632_ENABLE_REG, &strobe_state); + if (ret < 0) + goto out; + + *state = strobe_state & LM3632_FLASH_OUT_EN; + +out: + mutex_unlock(&priv->lock); + return ret; +} + +static int lm3632_flash_fault_get(struct led_classdev_flash *fled_cdev, + u32 *fault) +{ + struct lm3632_led *led = fled_cdev_to_led(fled_cdev); + + lm3632_read_faults(led); + + *fault = led->last_flag; + + return 0; +} + +static const struct led_flash_ops flash_ops = { + .flash_brightness_set = lm3632_flash_brightness_set, + .strobe_set = lm3632_strobe_set, + .strobe_get = lm3632_strobe_get, + .timeout_set = lm3632_flash_timeout_set, + .fault_get = lm3632_flash_fault_get, +}; + +static int lm3632_register_strobe_leds(struct lm3632_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 = LM3632_MIN_TIMEOUT_US; + setting->max = led->max_flash_timeout; + setting->step = LM3632_TIMEOUT_STEP_US; + setting->val = led->max_flash_timeout; + + setting = &led->fled_cdev.brightness; + setting->min = LM3632_MIN_STROBE_I_UA; + setting->max = led->flash_current_max; + setting->step = LM3632_STROBE_STEP_UA; + setting->val = led->flash_current_max; + + led_cdev = &led->fled_cdev.led_cdev; + led_cdev->name = led->led_name; + led_cdev->brightness_set_blocking = lm3632_torch_brightness_set; + led_cdev->max_brightness = led->torch_current_max; + + led_cdev->flags |= LED_DEV_CAP_FLASH; + + return led_classdev_flash_register(&led->priv->client->dev, &led->fled_cdev); +} + +static int lm3632_strobe_init(struct lm3632_led *led) +{ + struct lm3632 *priv = led->priv; + + if (priv->strobe_enable_gpio) + regmap_update_bits(priv->regmap, LM3632_IO_CTRL, + LM3632_HW_STROBE_EN, LM3632_HW_STROBE_EN); + + return regmap_update_bits(priv->regmap, LM3632_ENABLE_REG, + LM3632_FLASH_OUT_EN, ~LM3632_FLASH_OUT_EN); + +} + +static int lm3632_backlight_init(struct lm3632_led *led) +{ + struct lm3632 *priv = led->priv; + u8 bl_enable = 0; + int ret; + + if (led->led_strings[LM3632_BL12_ENABLE_SRC] == LM3632_BL_ENABLED) + bl_enable |= LM3632_BLED1_2_EN; + else if (led->led_strings[LM3632_BL1_ENABLE_SRC] == LM3632_BL_ENABLED) + bl_enable |= LM3632_BLED1_EN; + else + return -EINVAL; + + bl_enable |= LM3632_BL_EN; + + /* Power up default is on so lets set it to off */ + ret = ti_lmu_common_set_brightness(&led->lmu_data, LED_OFF); + if (ret) { + dev_err(&priv->client->dev, "Cannot write brightness\n"); + return ret; + } + + return regmap_update_bits(priv->regmap, LM3632_ENABLE_REG, + LM3632_BLED1_2_MASK, bl_enable); +} + +static int lm3632_parse_node(struct lm3632 *priv) +{ + struct fwnode_handle *child = NULL; + struct lm3632_led *led; + int ret = -ENODEV; + const char *name; + u32 led_mode; + size_t i = 0; + + priv->strobe_enable_gpio = device_property_present(&priv->client->dev, + "hw-strobe"); + + device_for_each_child_node(priv->dev, child) { + ret = fwnode_property_read_u32(child, "reg", &led_mode); + if (ret) { + dev_err(&priv->client->dev, "reg DT property missing\n"); + goto out_err; + } + + if (led_mode > LM3632_MODE_TORCH || + led_mode < LM3632_MODE_BL) { + dev_warn(&priv->client->dev, "Invalid led mode requested\n"); + ret = -EINVAL; + goto out_err; + } + + led = &priv->leds[i]; + led->priv = priv; + + ret = fwnode_property_read_string(child, "label", &name); + if (ret) { + if (led_mode == LM3632_MODE_TORCH) + name = "torch"; + else + name = "backlight"; + } + + snprintf(led->led_name, sizeof(led->led_name), + "%s:%s", priv->client->name, name); + + if (led_mode == LM3632_MODE_TORCH) { + ret = fwnode_property_read_u32(child, "led-max-microamp", + &led->torch_current_max); + if (ret) { + dev_warn(&priv->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(&priv->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(&priv->client->dev, + "flash-max-timeout-us DT property missing\n"); + goto out_err; + } + ret = lm3632_strobe_init(led); + if (ret) { + dev_err(&priv->client->dev, "failed to init strobe\n"); + continue; + } + + ret = lm3632_register_strobe_leds(led); + if (ret) { + dev_warn(&priv->client->dev, + "Failed to register flash LEDs\n"); + goto out_err; + } + + } else if (led_mode == LM3632_MODE_BL) { + + ret = fwnode_property_read_u32_array(child, "led-sources", + led->led_strings, + LM3632_NUM_OF_BL_STRINGS); + if (ret) { + dev_err(&priv->client->dev, "led-sources property missing\n"); + continue; + } + + led->led_dev.name = led->led_name; + led->led_dev.brightness_set_blocking = lm3632_backlight_brightness_set; + led->lmu_data.regmap = priv->regmap; + led->lmu_data.max_brightness = MAX_BRIGHTNESS_11BIT; + led->lmu_data.lsb_brightness_reg = LM3633_BL_BRT_LSB; + led->lmu_data.msb_brightness_reg = LM3633_BL_BRT_MSB; + led->lmu_data.enable_reg = LM3632_ENABLE_REG; + + ret = lm3632_backlight_init(led); + if (ret) { + dev_err(&priv->client->dev, "failed to init backlight\n"); + continue; + } + + ret = devm_led_classdev_register(priv->dev, &led->led_dev); + if (ret) { + dev_err(&priv->client->dev, "failed to register backlight\n"); + continue; + } + } else { + dev_warn(&priv->client->dev, + "Failed to register flash LEDs\n"); + goto out_err; + } + + i++; + } +out_err: + fwnode_handle_put(child); + return ret; +} + +static int lm3632_probe(struct i2c_client *client) +{ + struct lm3632 *led; + int count; + int ret; + + count = device_get_child_node_count(&client->dev); + if (!count) { + dev_err(&client->dev, "LEDs are not defined in device tree!"); + return -ENODEV; + } + + led = devm_kzalloc(&client->dev, struct_size(led, leds, count), + GFP_KERNEL); + if (!led) + return -ENOMEM; + + led->client = client; + led->dev = &client->dev; + i2c_set_clientdata(client, led); + + led->regmap = devm_regmap_init_i2c(client, &lm3632_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 lm3632_parse_node(led); +} + +static int lm3632_remove(struct i2c_client *client) +{ + struct lm3632 *led = i2c_get_clientdata(client); + +/* led_classdev_flash_unregister(&led->fled_cdev);*/ + mutex_destroy(&led->lock); + + return regmap_update_bits(led->regmap, LM3632_ENABLE_REG, + LM3632_ENABLE_MASK, 0x00); +} + +static const struct i2c_device_id lm3632_id[] = { + { "LM3632", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, lm3632_id); + +static const struct of_device_id of_lm3632_leds_match[] = { + { .compatible = "ti,lm3632", }, + { } +}; +MODULE_DEVICE_TABLE(of, of_lm3632_leds_match); + +static struct i2c_driver lm3632_i2c_driver = { + .driver = { + .name = "lm3632", + .of_match_table = of_lm3632_leds_match, + }, + .probe_new = lm3632_probe, + .remove = lm3632_remove, + .id_table = lm3632_id, +}; +module_i2c_driver(lm3632_i2c_driver); + +MODULE_DESCRIPTION("Texas Instruments Flash Lighting driver for LM3632"); +MODULE_AUTHOR("Dan Murphy "); +MODULE_LICENSE("GPL v2");