From patchwork Tue Oct 23 17:06:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Murphy X-Patchwork-Id: 149454 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp952946ljp; Tue, 23 Oct 2018 10:06:44 -0700 (PDT) X-Google-Smtp-Source: ACcGV62YsdUynAVxgX8Fb8qh0asqga920bYJ9oo3UxqRs37o0ObrRK6VxKrJaRecxxxw61XEnbbI X-Received: by 2002:a63:d64b:: with SMTP id d11-v6mr47034483pgj.450.1540314404090; Tue, 23 Oct 2018 10:06:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540314404; cv=none; d=google.com; s=arc-20160816; b=Pz2MvwX9qAHnhifqQ/GlHebnsTuT32X/LvVUz/dFu5yn5EKadynvj0njwe/lNOcZZp O3RcOyEPzestmntEmEBXtp5qV/l3uMAHs26US5Jn2YmeZ4nbRQ/sqk4+qXqYQP47KMHM M9LYZ6Z+GQathHx+aLkMF0L5o1hY4MHcZAfSEr7yoO0UJ21mcTl+lJp+NXShWkKcgLIx AmAyhwxWhTwLP9gI9zrGLVcxQz5uj4XiEQ4pcZm9HScvfsPrUNN8zeZY95eTX3dcZNHi Zx9EMtvQRinNe8TDjSR2iH8yKq7jncWa7Ow68/i6Y9EG8/fmzRKBaLFbT6qL31yvkktP oOxw== 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; bh=VC7TZuguLI9ukAtsC+n84IBWmdqPgQyyeU+b3me6o5A=; b=V7YkRTdRsd+3spumS7VeBu43QFAj9mDAreXfmmvgPGYyx6s/c4Acle6vY6rH1R4p8F 6thTMVeydaKiBscuG/VNT6HBsXz1Pf2Wo1JTBgcIY0yZIswR+jO5efptxrTk6xtYRx8c 2SC0/ioGq3Y8VJIkySWcxxiBLaDtADaYiS4TdqnsH6ZNblZkBhFtJLGwB2oR3tEpR4HP NTgu8z+/L3KK+uo+1Le/ADpUICW0drIyy6t83LHeSDzt8sUsQWPQ1RqCcgKZAJA8TKVc C2U/m+hbq3Er0pM4+H0eQTRlDnkMMWo1jhGN9JbCrj3HiJ7zwFVxXlfnS85viHOMgUKY HsCA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=NONE dis=QUARANTINE) 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 a7-v6si1772400pln.66.2018.10.23.10.06.43; Tue, 23 Oct 2018 10:06:44 -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; 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=fail (p=QUARANTINE sp=NONE dis=QUARANTINE) header.from=ti.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727730AbeJXBa4 (ORCPT + 1 other); Tue, 23 Oct 2018 21:30:56 -0400 Received: from fllv0016.ext.ti.com ([198.47.19.142]:53580 "EHLO fllv0016.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728180AbeJXBa4 (ORCPT ); Tue, 23 Oct 2018 21:30:56 -0400 Received: from lelv0266.itg.ti.com ([10.180.67.225]) by fllv0016.ext.ti.com (8.15.2/8.15.2) with ESMTP id w9NH6YXn056808; Tue, 23 Oct 2018 12:06:34 -0500 Received: from DLEE114.ent.ti.com (dlee114.ent.ti.com [157.170.170.25]) by lelv0266.itg.ti.com (8.15.2/8.15.2) with ESMTPS id w9NH6YPi047549 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 23 Oct 2018 12:06:34 -0500 Received: from DLEE108.ent.ti.com (157.170.170.38) by DLEE114.ent.ti.com (157.170.170.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Tue, 23 Oct 2018 12:06:34 -0500 Received: from dflp32.itg.ti.com (10.64.6.15) by DLEE108.ent.ti.com (157.170.170.38) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.1.1466.3 via Frontend Transport; Tue, 23 Oct 2018 12:06:33 -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 w9NH6XHJ005546; Tue, 23 Oct 2018 12:06:33 -0500 Received: from localhost (a0272616local-lt.dhcp.ti.com [172.22.171.190]) by legion.dal.design.ti.com (8.11.7p1+Sun/8.11.7) with ESMTP id w9NH6Xx15360; Tue, 23 Oct 2018 12:06:33 -0500 (CDT) From: Dan Murphy To: , , CC: , , , , , Dan Murphy Subject: [PATCH v4 4/7] leds: lm3697: Introduce the lm3697 driver Date: Tue, 23 Oct 2018 12:06:20 -0500 Message-ID: <20181023170623.31820-4-dmurphy@ti.com> X-Mailer: git-send-email 2.12.2 In-Reply-To: <20181023170623.31820-1-dmurphy@ti.com> References: <20181023170623.31820-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 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 | 381 +++++++++++++++++++++++++++++++++++++ 3 files changed, 389 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 853014dbb1c2..7d9a98044be4 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..1ed724fb0c43 --- /dev/null +++ b/drivers/leds/leds-lm3697.c @@ -0,0 +1,381 @@ +// 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 + +#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");