From patchwork Tue Aug 7 16:04:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Murphy X-Patchwork-Id: 143594 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp4678579ljj; Tue, 7 Aug 2018 09:04:57 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdM88vG1z7m5KU9wK6RzJmJ84tPasVSr11bsA5Gq9MZujkUmY5ClkRA7ZtAd/VYhTeJtg53 X-Received: by 2002:a63:dc53:: with SMTP id f19-v6mr19412010pgj.56.1533657897660; Tue, 07 Aug 2018 09:04:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533657897; cv=none; d=google.com; s=arc-20160816; b=OSicTnT90MGkyOuem8gVg0LGrPoEYoqLQE4we29S0xyqPi9Jm9Qj6UlRIVZ8rwcRpW NA5qtnycqH1SJCfALZjzIvByQNkWgKMyyVSjQWElKaA8Sa9+SHEzUmRXRwMuvyo783r/ JM8G2VKjSzC6Gd6cO9niJNnGeWq/C89EBZbPPIZAT59tSDElH/Okat3RfjHZbsaCFBcm XFBb1haocpQmqPAiI+Z+XAsDOuvwBjUa+RMdL34JF6t7UjNki28uVG8qIu4vvKXqYWfF 4J/AmzbmNhftDqb2LZFaPwdv7AD6W8QMEMsv59HVJMli9Ue3E87+q8cO8wmKbcJ68ALw b6ug== 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=nWVwsNUGVlRLv6HAtUqAH0X0svVk6m/IuLDQtyXd0Xc=; b=SRrP19yL3c3sMkerWgAnTVdqk74rKbqdnbzAPq7C6Cf4nsKQ6GnxO+7Ccx5Qtz+cz2 8tf5rjM2fRjaTtBLGUtLqP1FWuCza0nkrRIGBscbnNj8w6SjQat7Oixaivu/pW+OcKNQ bpX7mgJvZoVZLu7isEe3QnrTMaptbk/1AY2pd9NH/ONrJYkzggxl10OotWxTYizfONmx wiIm/vPFuEsIBmckms6Yj5fYd6NmwssWL4xgkmsGAOFICurQnJxa4K4qgbvVfxojZ3hi DINaEc+vuSpkQM3Eu29TCzXr9aJraXE0DfWafJ1qPfr6v+vDeqwhUfy85qS77G08TQf+ R7TA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=J+5a3aMu; 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 o188-v6si1866051pfo.236.2018.08.07.09.04.57; Tue, 07 Aug 2018 09:04:57 -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=J+5a3aMu; 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 S2389310AbeHGSTx (ORCPT + 31 others); Tue, 7 Aug 2018 14:19:53 -0400 Received: from fllv0015.ext.ti.com ([198.47.19.141]:57938 "EHLO fllv0015.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389096AbeHGSTx (ORCPT ); Tue, 7 Aug 2018 14:19:53 -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 w77G4qAI035926; Tue, 7 Aug 2018 11:04:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1533657892; bh=nWVwsNUGVlRLv6HAtUqAH0X0svVk6m/IuLDQtyXd0Xc=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=J+5a3aMu6/mrA8vyMKC0L3mGyCrvYcedJqxAPFUKZqVHHdQN0E0F4Q65360vT9Ypc B2ZWqQvijkvNQbtZd3WG2l3M6DfT3vNo+wiU0Tig0J+CKLPNxUMLr2MtDplELNp6Fe feTjtG6i8jrqZ+0TmBNgDInPVzOnP5GW8YTGHT4s= Received: from DLEE112.ent.ti.com (dlee112.ent.ti.com [157.170.170.23]) by dflxv15.itg.ti.com (8.14.3/8.13.8) with ESMTP id w77G4qm0007211; Tue, 7 Aug 2018 11:04:52 -0500 Received: from DLEE107.ent.ti.com (157.170.170.37) by DLEE112.ent.ti.com (157.170.170.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Tue, 7 Aug 2018 11:04:52 -0500 Received: from dflp33.itg.ti.com (10.64.6.16) by DLEE107.ent.ti.com (157.170.170.37) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.1.1466.3 via Frontend Transport; Tue, 7 Aug 2018 11:04:52 -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 w77G4qYr012283; Tue, 7 Aug 2018 11:04:52 -0500 Received: from localhost (a0272616local-lt.dhcp.ti.com [172.22.157.47]) by legion.dal.design.ti.com (8.11.7p1+Sun/8.11.7) with ESMTP id w77G4px02691; Tue, 7 Aug 2018 11:04:51 -0500 (CDT) From: Dan Murphy To: , CC: , , , Dan Murphy Subject: [PATCH v2 2/2] leds: lm3697: Introduce the lm3697 driver Date: Tue, 7 Aug 2018 11:04:42 -0500 Message-ID: <20180807160442.8937-2-dmurphy@ti.com> X-Mailer: git-send-email 2.12.2 In-Reply-To: <20180807160442.8937-1-dmurphy@ti.com> References: <20180807160442.8937-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 lm3697 LED driver for backlighting and display. Datasheet location: http://www.ti.com/lit/ds/symlink/lm3697.pdf Signed-off-by: Dan Murphy --- v2 - Removed unneed 'of' calls in dt_parse, fixed comment, fixed checkpatch error, and change led registration - https://lore.kernel.org/patchwork/patch/971327/ drivers/leds/Kconfig | 9 + drivers/leds/Makefile | 1 + drivers/leds/leds-lm3697.c | 368 +++++++++++++++++++++++++++++++++++++ 3 files changed, 378 insertions(+) create mode 100644 drivers/leds/leds-lm3697.c -- 2.17.0.582.gccdcbd54c diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index 6e3a998f3370..efa8be180934 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -150,6 +150,15 @@ config LEDS_LM3642 converter plus 1.5A constant current driver for a high-current white LED. +config LEDS_LM3697 + tristate "LED support for LM3697 Chip" + depends on LEDS_CLASS && I2C + select REGMAP_I2C + help + This option enables support for LEDs connected to LM3697. + The LM3697 is an 11 bit high performance backlight driver for + keypad and display lighting. + config LEDS_LM3692X tristate "LED support for LM3692x Chips" depends on LEDS_CLASS && I2C && OF diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index 420b5d2cfa62..2813f089f3db 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -19,6 +19,7 @@ obj-$(CONFIG_LEDS_LOCOMO) += leds-locomo.o obj-$(CONFIG_LEDS_LM3530) += leds-lm3530.o obj-$(CONFIG_LEDS_LM3533) += leds-lm3533.o obj-$(CONFIG_LEDS_LM3642) += leds-lm3642.o +obj-$(CONFIG_LEDS_LM3697) += leds-lm3697.o obj-$(CONFIG_LEDS_MIKROTIK_RB532) += leds-rb532.o obj-$(CONFIG_LEDS_S3C24XX) += leds-s3c24xx.o obj-$(CONFIG_LEDS_NET48XX) += leds-net48xx.o diff --git a/drivers/leds/leds-lm3697.c b/drivers/leds/leds-lm3697.c new file mode 100644 index 000000000000..6384f1d0280e --- /dev/null +++ b/drivers/leds/leds-lm3697.c @@ -0,0 +1,368 @@ +// 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 +#include +#include +#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_CONTROL_A 0 +#define LM3697_CONTROL_B 1 + +#define LM3697_HVLED1_2_3_A 0 +#define LM3697_HVLED1_B_HVLED2_3_A 1 +#define LM3697_HVLED2_B_HVLED1_3_A 2 +#define LM3697_HVLED1_2_B_HVLED3_A 3 +#define LM3697_HVLED3_B_HVLED1_2_A 4 +#define LM3697_HVLED1_3_B_HVLED2_A 5 +#define LM3697_HVLED1_A_HVLED2_3_B 6 +#define LM3697_HVLED1_2_3_B 7 + +/** + * struct lm3697_led - + * @led_dev - LED class device + * @priv - Pointer to the device struct + * @control_bank - Control bank the LED is associated to. 0 is control bank A + * 1 is control bank B. + * @label - LED label + */ +struct lm3697_led { + struct led_classdev led_dev; + struct lm3697 *priv; + int control_bank; + char label[LED_MAX_NAME_SIZE]; +}; + +/** + * struct lm3697 - + * @lock - Lock for reading/writing the device + * @client - Pointer to the I2C client + * @dev - Pointer to the devices device struct + * @regmap - Devices register map + * @enable_gpio - Hardware enable gpio + * @regulator - LED supply regulator pointer + * @control_bank_config - Control bank configuration + * @leds - Array of LED strings. + */ +struct lm3697 { + struct mutex lock; + struct i2c_client *client; + struct device *dev; + struct regmap *regmap; + struct gpio_desc *enable_gpio; + struct regulator *regulator; + int control_bank_config; + 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_RBTREE, +}; + +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 brt_msb_reg, brt_lsb_reg, ctrl_en_val; + int led_brightness_lsb = (brt_val >> 5); + int ret; + + mutex_lock(&led->priv->lock); + + if (led->control_bank == LM3697_CONTROL_A) { + brt_msb_reg = LM3697_CTRL_A_BRT_MSB; + brt_lsb_reg = LM3697_CTRL_A_BRT_LSB; + ctrl_en_val = LM3697_CTRL_A_EN; + } else { + brt_msb_reg = LM3697_CTRL_B_BRT_MSB; + brt_lsb_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); + + if (ret) { + dev_err(&led->priv->client->dev, "Cannot write CTRL enable\n"); + goto out; + } + + ret = regmap_write(led->priv->regmap, brt_lsb_reg, led_brightness_lsb); + if (ret) { + dev_err(&led->priv->client->dev, "Cannot write LSB\n"); + goto out; + } + + ret = regmap_write(led->priv->regmap, brt_msb_reg, brt_val); + if (ret) { + dev_err(&led->priv->client->dev, "Cannot write MSB\n"); + goto out; + } +out: + mutex_unlock(&led->priv->lock); + return ret; +} + +static int lm3697_init(struct lm3697 *priv) +{ + int ret; + + if (priv->enable_gpio) + gpiod_direction_output(priv->enable_gpio, 1); + else + regmap_write(priv->regmap, LM3697_RESET, LM3697_SW_RESET); + + ret = regmap_write(priv->regmap, LM3697_OUTPUT_CONFIG, + priv->control_bank_config); + if (ret) { + dev_err(&priv->client->dev, "Cannot write MSB\n"); + goto out; + } + + ret = regmap_write(priv->regmap, LM3697_CTRL_ENABLE, 0x0); + if (ret) { + dev_err(&priv->client->dev, "Cannot write LSB\n"); + goto out; + } + +out: + return ret; +} + +static int lm3697_probe_dt(struct lm3697 *priv) +{ + struct fwnode_handle *child = NULL; + struct fwnode_handle *fwnode; + struct lm3697_led *led; + const char *name; + 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; + + fwnode = dev_fwnode(&priv->client->dev); + ret = fwnode_property_read_u32(fwnode, "control-bank-cfg", + &priv->control_bank_config); + if (ret) { + dev_err(&priv->client->dev, "reg DT property missing\n"); + goto out; + } + + if (priv->control_bank_config < LM3697_HVLED1_2_3_A || + priv->control_bank_config > LM3697_HVLED1_2_3_B) { + dev_err(&priv->client->dev, "Control bank configuration is out of range\n"); + ret = -EINVAL; + goto out; + } + + device_for_each_child_node(priv->dev, child) { + led = &priv->leds[i]; + + ret = fwnode_property_read_u32(child, "reg", &led->control_bank); + if (ret) { + dev_err(&priv->client->dev, "reg DT property missing\n"); + goto child_out; + } + + 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); + goto child_out; + } + + if (priv->control_bank_config == LM3697_HVLED1_2_3_A || + priv->control_bank_config == LM3697_HVLED1_2_3_B) + goto child_out; + + i++; + fwnode_handle_put(child); + } + +child_out: + fwnode_handle_put(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; + + ret = lm3697_init(led); + if (ret) + return ret; + + return ret; +} + +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 regulator\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");