Received: by 10.192.165.148 with SMTP id m20csp3513066imm; Mon, 23 Apr 2018 07:49:38 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/tVjaw4SgUeAYxEsVZRN1feIYlvbW0+rRHjuIrfnq0NkKEHMU02MFAQXAC3GftRtMaLAOM X-Received: by 10.101.77.199 with SMTP id q7mr17599240pgt.305.1524494978707; Mon, 23 Apr 2018 07:49:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524494978; cv=none; d=google.com; s=arc-20160816; b=EgYtfa76aGdasTyIiSr5Lu34Iism0F9/kZJ9KWm4UsBu3jza+cANQgPr+hmfz8vpe4 LV/WQnMa+ILIpLdiGbdaei308WncmkLehbod8m0YD1kpr528fIAH77PEyC2ou5O3bju+ m0+TZpIvAV1ZKuCnk0J5X+1s9EIoa/5xlKDjNv/uQn9TT2r3m84lD5XyNrABfgoaUpCI dD3QpcqG+QAk5yTf3eXrkaiamroRJ7OV1iMl/bG4RuqrKjoC1Lc8MUMzTBtsU0HaUyRw 5yYF3fAKBxd29HEUi29/dS5WcZDTYM85R+DGoK/68p8fKj4F0dwMfZisOfs29sEpe3U1 q4nA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=aGIu0IU8E+8dG4NgkXKr6DpLWuoyPOIIWAoTGKOE51U=; b=P7klUYhGuBKucZnwUTmseOi0Ji6jluv22169WTw+wLFbmvtO3p0g72qw7QN4vN4N5S bBrtfR9ipmtKegN9/ju7apgVkDBxTb4EORz56DKMznzHH1oCWBPzYyDMW/SEXTCqsv6M ZIAXbSidW8EiRG6wy3mp7+iQMfncllWs26VGmenUjlUmMxTNLCqSF1qN1HJX+tF9lgfh Rvnjdje/bYGGgQEfQ/4GMtXm3utgcWFdenLOGYXcsXFaE/sSAT+ilSxOS11lVCxvSkVg DwQgBMv9A9oK3osV8M3qMz9lHhhzl8zJ8PR6DGFORdRI0nF5UX2isBJZJfR85855YOJl SC2g== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g6si9708848pgq.321.2018.04.23.07.49.24; Mon, 23 Apr 2018 07:49:38 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755555AbeDWOr5 (ORCPT + 99 others); Mon, 23 Apr 2018 10:47:57 -0400 Received: from hermes.aosc.io ([199.195.250.187]:52671 "EHLO hermes.aosc.io" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755198AbeDWOr4 (ORCPT ); Mon, 23 Apr 2018 10:47:56 -0400 Received: from localhost (localhost [127.0.0.1]) (Authenticated sender: icenowy@aosc.io) by hermes.aosc.io (Postfix) with ESMTPSA id DEEEF5B2BA; Mon, 23 Apr 2018 14:47:51 +0000 (UTC) From: Icenowy Zheng To: Liam Girdwood , Mark Brown , Rob Herring , Maxime Ripard , Chen-Yu Tsai Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@googlegroups.com, Ondrej Jirman , Icenowy Zheng Subject: [PATCH v3 2/3] regulator: add support for SY8106A regulator Date: Mon, 23 Apr 2018 22:46:56 +0800 Message-Id: <20180423144657.63264-3-icenowy@aosc.io> In-Reply-To: <20180423144657.63264-1-icenowy@aosc.io> References: <20180423144657.63264-1-icenowy@aosc.io> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ondrej Jirman SY8106A is an I2C attached single output regulator made by Silergy Corp, which is used on several Allwinner H3/H5 SBCs to control the power supply of the ARM cores. Add a driver for it. Signed-off-by: Ondrej Jirman [Icenowy: Change commit message, remove enable/disable code, add default ramp_delay, add comment for go bit, add code for fixed mode voltage] Signed-off-by: Icenowy Zheng Reviewed-by: Chen-Yu Tsai --- Changes in v3: - Return the fixed voltage defined in device tree when I2C regulating not enabled (in this situation the register may contain no valid voltage data). Changes in v2: - Dropped the enable/disable code. - Added default ramp_delay value. - Added comment for the "go bit". MAINTAINERS | 6 ++ drivers/regulator/Kconfig | 7 ++ drivers/regulator/Makefile | 2 +- drivers/regulator/sy8106a-regulator.c | 175 ++++++++++++++++++++++++++++++++++ 4 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 drivers/regulator/sy8106a-regulator.c diff --git a/MAINTAINERS b/MAINTAINERS index a33643739820..e0cb78568458 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13464,6 +13464,12 @@ S: Supported F: net/switchdev/ F: include/net/switchdev.h +SY8106A REGULATOR DRIVER +M: Icenowy Zheng +S: Maintained +F: drivers/regulator/sy8106a-regulator.c +F: Documentation/devicetree/bindings/regulator/sy8106a-regulator.txt + SYNC FILE FRAMEWORK M: Sumit Semwal R: Gustavo Padovan diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig index 097f61784a7d..4efae3b7e746 100644 --- a/drivers/regulator/Kconfig +++ b/drivers/regulator/Kconfig @@ -801,6 +801,13 @@ config REGULATOR_STW481X_VMMC This driver supports the internal VMMC regulator in the STw481x PMIC chips. +config REGULATOR_SY8106A + tristate "Silergy SY8106A regulator" + depends on I2C && (OF || COMPILE_TEST) + select REGMAP_I2C + help + This driver supports SY8106A single output regulator. + config REGULATOR_TPS51632 tristate "TI TPS51632 Power Regulator" depends on I2C diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile index 590674fbecd7..d81fb02bd6e9 100644 --- a/drivers/regulator/Makefile +++ b/drivers/regulator/Makefile @@ -100,6 +100,7 @@ obj-$(CONFIG_REGULATOR_SC2731) += sc2731-regulator.o obj-$(CONFIG_REGULATOR_SKY81452) += sky81452-regulator.o obj-$(CONFIG_REGULATOR_STM32_VREFBUF) += stm32-vrefbuf.o obj-$(CONFIG_REGULATOR_STW481X_VMMC) += stw481x-vmmc.o +obj-$(CONFIG_REGULATOR_SY8106A) += sy8106a-regulator.o obj-$(CONFIG_REGULATOR_TI_ABB) += ti-abb-regulator.o obj-$(CONFIG_REGULATOR_TPS6105X) += tps6105x-regulator.o obj-$(CONFIG_REGULATOR_TPS62360) += tps62360-regulator.o @@ -125,5 +126,4 @@ obj-$(CONFIG_REGULATOR_WM8350) += wm8350-regulator.o obj-$(CONFIG_REGULATOR_WM8400) += wm8400-regulator.o obj-$(CONFIG_REGULATOR_WM8994) += wm8994-regulator.o - ccflags-$(CONFIG_REGULATOR_DEBUG) += -DDEBUG diff --git a/drivers/regulator/sy8106a-regulator.c b/drivers/regulator/sy8106a-regulator.c new file mode 100644 index 000000000000..6dbe41044ab9 --- /dev/null +++ b/drivers/regulator/sy8106a-regulator.c @@ -0,0 +1,176 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * sy8106a-regulator.c - Regulator device driver for SY8106A + * + * Copyright (C) 2016 Ondřej Jirman + * Copyright (c) 2017-2018 Icenowy Zheng + */ + +#include +#include +#include +#include +#include +#include + +#define SY8106A_REG_VOUT1_SEL 0x01 +#define SY8106A_REG_VOUT_COM 0x02 +#define SY8106A_REG_VOUT1_SEL_MASK 0x7f +#define SY8106A_DISABLE_REG BIT(0) +/* + * The I2C controlled voltage will only work when this bit is set; otherwise + * it will behave like a fixed regulator. + */ +#define SY8106A_GO_BIT BIT(7) + +struct sy8106a { + struct regulator_dev *rdev; + struct regmap *regmap; + u32 fixed_voltage; +}; + +static const struct regmap_config sy8106a_regmap_config = { + .reg_bits = 8, + .val_bits = 8, +}; + +static int sy8106a_set_voltage_sel(struct regulator_dev *rdev, unsigned int sel) +{ + /* We use our set_voltage_sel in order to avoid unnecessary I2C + * chatter, because the regulator_get_voltage_sel_regmap using + * apply_bit would perform 4 unnecessary transfers instead of one, + * increasing the chance of error. + */ + return regmap_write(rdev->regmap, rdev->desc->vsel_reg, + sel | SY8106A_GO_BIT); +} + +static int sy8106a_get_voltage_sel(struct regulator_dev *rdev) +{ + struct sy8106a *chip = rdev_get_drvdata(rdev); + unsigned int reg; + int error; + + error = regmap_read(rdev->regmap, rdev->desc->vsel_reg, ®); + if (error) + return error; + + if (reg & SY8106A_GO_BIT) + return reg & rdev->desc->vsel_mask; + else + return (chip->fixed_voltage - rdev->desc->min_uV) / + rdev->desc->uV_step; +} + +static const struct regulator_ops sy8106a_ops = { + .set_voltage_sel = sy8106a_set_voltage_sel, + .set_voltage_time_sel = regulator_set_voltage_time_sel, + .get_voltage_sel = sy8106a_get_voltage_sel, + .list_voltage = regulator_list_voltage_linear, + /* Enabling/disabling the regulator is not yet implemented */ +}; + +/* Default limits measured in millivolts */ +#define SY8106A_MIN_MV 680 +#define SY8106A_MAX_MV 1950 +#define SY8106A_STEP_MV 10 + +static const struct regulator_desc sy8106a_reg = { + .name = "SY8106A", + .id = 0, + .ops = &sy8106a_ops, + .type = REGULATOR_VOLTAGE, + .n_voltages = ((SY8106A_MAX_MV - SY8106A_MIN_MV) / SY8106A_STEP_MV) + 1, + .min_uV = (SY8106A_MIN_MV * 1000), + .uV_step = (SY8106A_STEP_MV * 1000), + .vsel_reg = SY8106A_REG_VOUT1_SEL, + .vsel_mask = SY8106A_REG_VOUT1_SEL_MASK, + /* + * This ramp_delay is a conservative default value which works on + * H3/H5 boards VDD-CPUX situations. + */ + .ramp_delay = 200, + .owner = THIS_MODULE, +}; + +/* + * I2C driver interface functions + */ +static int sy8106a_i2c_probe(struct i2c_client *i2c, + const struct i2c_device_id *id) +{ + struct sy8106a *chip; + struct device *dev = &i2c->dev; + struct regulator_dev *rdev = NULL; + struct regulator_config config = { }; + int error; + + chip = devm_kzalloc(&i2c->dev, sizeof(struct sy8106a), GFP_KERNEL); + if (!chip) + return -ENOMEM; + + error = of_property_read_u32(dev->of_node, "silergy,fixed-microvolt", + &chip->fixed_voltage); + if (error) + return error; + + chip->regmap = devm_regmap_init_i2c(i2c, &sy8106a_regmap_config); + if (IS_ERR(chip->regmap)) { + error = PTR_ERR(chip->regmap); + dev_err(dev, "Failed to allocate register map: %d\n", error); + return error; + } + + config.dev = &i2c->dev; + config.regmap = chip->regmap; + config.driver_data = chip; + + config.of_node = dev->of_node; + config.init_data = of_get_regulator_init_data(dev, dev->of_node, + &sy8106a_reg); + + if (!config.init_data) + return -ENOMEM; + + /* Probe regulator */ + rdev = devm_regulator_register(&i2c->dev, &sy8106a_reg, &config); + if (IS_ERR(rdev)) { + error = PTR_ERR(rdev); + dev_err(&i2c->dev, "Failed to register SY8106A regulator: %d\n", error); + return error; + } + + chip->rdev = rdev; + + i2c_set_clientdata(i2c, chip); + + return 0; +} + +static const struct of_device_id sy8106a_i2c_of_match[] = { + { .compatible = "silergy,sy8106a" }, + { }, +}; +MODULE_DEVICE_TABLE(of, sy8106a_i2c_of_match); + +static const struct i2c_device_id sy8106a_i2c_id[] = { + { "sy8106a", 0 }, + { }, +}; +MODULE_DEVICE_TABLE(i2c, sy8106a_i2c_id); + +static struct i2c_driver sy8106a_regulator_driver = { + .driver = { + .name = "sy8106a", + .of_match_table = of_match_ptr(sy8106a_i2c_of_match), + }, + .probe = sy8106a_i2c_probe, + .id_table = sy8106a_i2c_id, +}; + +module_i2c_driver(sy8106a_regulator_driver); + +MODULE_AUTHOR("Ondřej Jirman "); +MODULE_AUTHOR("Icenowy Zheng "); +MODULE_DESCRIPTION("Regulator device driver for Silergy SY8106A"); +MODULE_LICENSE("GPL"); -- 2.15.1