From patchwork Thu Mar 25 18:44:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ying-Chun Liu X-Patchwork-Id: 408851 Delivered-To: patch@linaro.org Received: by 2002:a02:8562:0:0:0:0:0 with SMTP id g89csp782735jai; Thu, 25 Mar 2021 11:44:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx8IWOzBun83BHforkxeX5jEIlZ0uU5J0ZnP5J0xX7xGYGxqt47LS6glJ4BwsfOxAbnGsDg X-Received: by 2002:a17:907:2b03:: with SMTP id gc3mr11106477ejc.448.1616697876459; Thu, 25 Mar 2021 11:44:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616697876; cv=none; d=google.com; s=arc-20160816; b=qyTMsgvvVe3rQhxZwYe7ZCeF/JB4EZMLf+eL/gPPOn3GwctF2foXmOHtCHwBtfCwHC hDxe1xwjAI6qoNsk/IHz25T9OhLHicFC0uyxJAntCuf59IOlYnCk8Y5UjfJuhr6CoIm9 yv9yACtSqyM4iNs2kk5r09qEY4v3ttPNTTXwnkBFyAxrNupbn2TtI1Im2uMBf99N23Gh MOsIt5YY968AhOmpy3fCTDRySaip8wTojRBCF6r7rU+XgGmouN4jfMcyG7eoKX6FmlJp tMkREzL6Pws1sKsgVY956OqhS4MDvOgmqcs3qtm4XAqYgU31f/S1uJmp7QXTMOlHIJbL QFtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ZSmOr1qzOeqjcd6QQIMyi+VB3wqwHTr6j6kaq/PsaUQ=; b=Oro3c1eOiuP43j7D4aIYxyAUb0DhY+e6KIRnIDUcGwjffQa+fPuGWnqJPVvjHOW6Sm VaNGICzxE4cZVb4xMF1uBoT2TZnOYDcNlp3ix2WTUBJccYf1igecvqcGAfC+WvgLEQGP WLrHHtvr5jIN9/1WQHEFdviyqppoTlHV2Wfs4fGLZMQwGWUQYWRi09XNkRNSlL6HCcZL evwbcusA0W/Qzg2tGP1L0uzxnZaWkBFdcAOvV2MVgdkEvGQp0Ur3EV7uaP5B/95JLMsl Z8wZLUK5OntrFQLGZ9qtb0g9CK3bb2LOcg5cNL0xiCSXVSSuEXSupL+m99dK+D6761kI StWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=uhPULeTD; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id v5si4722154edx.253.2021.03.25.11.44.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 11:44:36 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=uhPULeTD; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 618E6805BF; Thu, 25 Mar 2021 19:44:27 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="uhPULeTD"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0CDBA805BF; Thu, 25 Mar 2021 19:44:23 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id DDE43805BF for ; Thu, 25 Mar 2021 19:44:18 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=grandpaul@gmail.com Received: by mail-pj1-x1035.google.com with SMTP id x21-20020a17090a5315b029012c4a622e4aso570806pjh.2 for ; Thu, 25 Mar 2021 11:44:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZSmOr1qzOeqjcd6QQIMyi+VB3wqwHTr6j6kaq/PsaUQ=; b=uhPULeTDy5TlrhObcd7H7s/XfetpFZImTQcCTVYQLFe9LA6gipHC8REfIQ1WryT/CB n+28svZoPVCVp7N1MPkpycO5vAg4abdleBMWbS0He7tkZnKiJSgqwQrcmGePGm+/yfwP QWOGD2d7Ka5jGH2csDsKqfd/dbm0v/R2uStKybvRKfui+6z+Ttf7x2BSBw9hXRUnCmiL bW5xeSmXz5H6mRCH3mYsP/x+1vTgJqckW+keoV9gkBq+o+OYl6PafvSVtZAV5QX0P2wU yv051yv66Se/cZwN5NuGTpwwt0jHbBaJ766HFbtuTo6Y5+8GbKYOlrqsiayzeNstggMX C43g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZSmOr1qzOeqjcd6QQIMyi+VB3wqwHTr6j6kaq/PsaUQ=; b=gp3jcjNU6wsOSrM0uG2lZ9P8otMwkuGg7WDWG0uaN2bKxOiZEwrpfslhKY5x5HnsZW t+7ZAQJ+LNfa952qhU4a1k5onvwVCPtSNF+DoR1A7fLUvXffEOiL1/iKjW625DeIzVCl YUXk1Hk70uyvHUUsdnK0eCf0kFAxwkEGaxJ4n4E9InS5BK4ISUrYJwiolpxe+QQ5x/tK pL5MQV/mFV42MMuXH5HN+Ufn8UFxE0d/Kqo7HEouxgPHpJFgXbbPpu3r1mabl9ZwUija OwgkOHIy7oM88I007GEMHAg6/0GyIsQ1EfQqrgG+wievcOpkhpDsqcno/GrxKPZvBLFh GhAg== X-Gm-Message-State: AOAM531kwXhGPtRfCLxDLT7cKHjR2AgiAUtnk7sznSd0uIaZ1c+Y52rl bVdR855w1qYdcapPcOVpcoLvYe4wqTfW+Q== X-Received: by 2002:a17:90a:68cf:: with SMTP id q15mr10257792pjj.231.1616697856950; Thu, 25 Mar 2021 11:44:16 -0700 (PDT) Received: from localhost (host-111-184-129-17.dynamic.kbtelecom.net. [111.184.129.17]) by smtp.gmail.com with ESMTPSA id b140sm6745003pfb.98.2021.03.25.11.44.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 11:44:16 -0700 (PDT) From: Ying-Chun Liu To: u-boot@lists.denx.de Cc: "Ying-Chun Liu (PaulLiu)" , Fabio Estevam , Jaehoon Chung , Peng Fan , Sean Anderson Subject: [PATCH v3 1/2] power: regulator: add driver for ANATOP regulator Date: Fri, 26 Mar 2021 02:44:06 +0800 Message-Id: <20210325184407.37915-2-grandpaul@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210325184407.37915-1-grandpaul@gmail.com> References: <20210325184407.37915-1-grandpaul@gmail.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.4 at phobos.denx.de X-Virus-Status: Clean From: "Ying-Chun Liu (PaulLiu)" Anatop is an integrated regulator inside i.MX6 SoC. There are 3 digital regulators which controls PU, CORE (ARM), and SOC. And 3 analog regulators which controls 1P1, 2P5, 3P0 (USB). This patch adds the Anatop regulator driver. Signed-off-by: Ying-Chun Liu (PaulLiu) Cc: Fabio Estevam Cc: Jaehoon Chung Cc: Peng Fan Cc: Sean Anderson --- v2: add functions for set selector and delay. Define ANATOP_REGULATOR_STEP v3: add vin-supply. move regmap retrival to probe --- drivers/power/regulator/Kconfig | 10 + drivers/power/regulator/Makefile | 1 + drivers/power/regulator/anatop_regulator.c | 287 +++++++++++++++++++++ 3 files changed, 298 insertions(+) create mode 100644 drivers/power/regulator/anatop_regulator.c -- 2.30.2 diff --git a/drivers/power/regulator/Kconfig b/drivers/power/regulator/Kconfig index fbbea18c7d..1cd1f3f5ed 100644 --- a/drivers/power/regulator/Kconfig +++ b/drivers/power/regulator/Kconfig @@ -312,6 +312,16 @@ config DM_REGULATOR_STPMIC1 by the PMIC device. This driver is controlled by a device tree node which includes voltage limits. +config DM_REGULATOR_ANATOP + bool "Enable driver for ANATOP regulators" + depends on DM_REGULATOR + select REGMAP + select SYSCON + help + Enable support for the Freescale i.MX on-chip ANATOP LDOs + regulators. It is recommended that this option be enabled on + i.MX6 platform. + config SPL_DM_REGULATOR_STPMIC1 bool "Enable driver for STPMIC1 regulators in SPL" depends on SPL_DM_REGULATOR && PMIC_STPMIC1 diff --git a/drivers/power/regulator/Makefile b/drivers/power/regulator/Makefile index 9d58112dcb..e7198da911 100644 --- a/drivers/power/regulator/Makefile +++ b/drivers/power/regulator/Makefile @@ -30,3 +30,4 @@ obj-$(CONFIG_DM_REGULATOR_TPS65910) += tps65910_regulator.o obj-$(CONFIG_DM_REGULATOR_TPS62360) += tps62360_regulator.o obj-$(CONFIG_$(SPL_)DM_REGULATOR_STPMIC1) += stpmic1.o obj-$(CONFIG_DM_REGULATOR_TPS65941) += tps65941_regulator.o +obj-$(CONFIG_$(SPL_)DM_REGULATOR_ANATOP) += anatop_regulator.o diff --git a/drivers/power/regulator/anatop_regulator.c b/drivers/power/regulator/anatop_regulator.c new file mode 100644 index 0000000000..af5f9d2a2b --- /dev/null +++ b/drivers/power/regulator/anatop_regulator.c @@ -0,0 +1,287 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved. + * Copyright (C) 2021 Linaro + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LDO_RAMP_UP_UNIT_IN_CYCLES 64 /* 64 cycles per step */ +#define LDO_RAMP_UP_FREQ_IN_MHZ 24 /* cycle based on 24M OSC */ + +#define LDO_POWER_GATE 0x00 +#define LDO_FET_FULL_ON 0x1f + +#define BIT_WIDTH_MAX 32 + +#define ANATOP_REGULATOR_STEP 25000 +#define MIN_DROPOUT_UV 125000 + +struct anatop_regulator { + const char *name; + struct regmap *regmap; + struct udevice *supply; + u32 control_reg; + u32 vol_bit_shift; + u32 vol_bit_width; + u32 min_bit_val; + u32 min_voltage; + u32 max_voltage; + u32 delay_reg; + u32 delay_bit_shift; + u32 delay_bit_width; +}; + +static u32 anatop_get_bits(struct udevice *dev, u32 addr, int bit_shift, + int bit_width) +{ + const struct anatop_regulator *anatop_reg = dev_get_plat(dev); + struct regmap *regmap; + int err; + u32 val, mask; + + regmap = anatop_reg->regmap; + + if (bit_width == BIT_WIDTH_MAX) + mask = ~0; + else + mask = (1 << bit_width) - 1; + + err = regmap_read(regmap, addr, &val); + if (err) + return err; + + val = (val >> bit_shift) & mask; + + return val; +} + +static int anatop_set_bits(struct udevice *dev, u32 addr, int bit_shift, + int bit_width, u32 data) +{ + const struct anatop_regulator *anatop_reg = dev_get_plat(dev); + struct regmap *regmap; + int err; + u32 val, mask; + + regmap = anatop_reg->regmap; + + if (bit_width == 32) + mask = ~0; + else + mask = (1 << bit_width) - 1; + + err = regmap_read(regmap, addr, &val); + if (err) { + dev_dbg(dev, + "%s: cannot read reg (%d)\n", __func__, + err); + return err; + } + val = val & ~(mask << bit_shift); + err = regmap_write(regmap, addr, (data << bit_shift) | val); + if (err) { + dev_dbg(dev, + "%s: cannot wrie reg (%d)\n", __func__, + err); + return err; + } + + return 0; +} + +static u32 anatop_get_selector(struct udevice *dev, + const struct anatop_regulator *anatop_reg) +{ + u32 val = anatop_get_bits(dev, + anatop_reg->control_reg, + anatop_reg->vol_bit_shift, + anatop_reg->vol_bit_width); + + val = val - anatop_reg->min_bit_val; + + return val; +} + +static int anatop_set_voltage(struct udevice *dev, int uV) +{ + const struct anatop_regulator *anatop_reg = dev_get_plat(dev); + u32 val; + u32 sel; + int ret; + + dev_dbg(dev, "%s: uv %d, min %d, max %d\n", __func__, + uV, anatop_reg->min_voltage, + anatop_reg->max_voltage); + + if (uV < anatop_reg->min_voltage) + return -EINVAL; + + if (!anatop_reg->control_reg) + return -ENOTSUPP; + + sel = DIV_ROUND_UP(uV - anatop_reg->min_voltage, + ANATOP_REGULATOR_STEP); + if (sel * ANATOP_REGULATOR_STEP + anatop_reg->min_voltage > + anatop_reg->max_voltage) + return -EINVAL; + val = anatop_reg->min_bit_val + sel; + dev_dbg(dev, "%s: calculated val %d\n", __func__, val); + + if (anatop_reg->supply) { + ret = regulator_set_value(anatop_reg->supply, + uV + MIN_DROPOUT_UV); + if (ret) + return ret; + } + + ret = anatop_set_bits(dev, + anatop_reg->control_reg, + anatop_reg->vol_bit_shift, + anatop_reg->vol_bit_width, + val); + + return ret; +} + +static int anatop_get_voltage(struct udevice *dev) +{ + const struct anatop_regulator *anatop_reg = dev_get_plat(dev); + u32 sel; + + sel = anatop_get_selector(dev, anatop_reg); + + return sel * ANATOP_REGULATOR_STEP + anatop_reg->min_voltage; +} + +static const struct dm_regulator_ops anatop_regulator_ops = { + .set_value = anatop_set_voltage, + .get_value = anatop_get_voltage, +}; + +static int anatop_regulator_probe(struct udevice *dev) +{ + struct anatop_regulator *anatop_reg; + struct dm_regulator_uclass_plat *uc_pdata; + struct udevice *syscon; + int ret = 0; + u32 val; + + anatop_reg = dev_get_plat(dev); + uc_pdata = dev_get_uclass_plat(dev); + + anatop_reg->name = ofnode_read_string(dev_ofnode(dev), + "regulator-name"); + if (!anatop_reg->name) + return log_msg_ret("regulator-name", -EINVAL); + + ret = device_get_supply_regulator(dev, "vin-supply", + &anatop_reg->supply); + if (!ret) { + ret = regulator_set_enable(anatop_reg->supply, true); + if (ret) + return ret; + } + + ret = dev_read_u32(dev, + "anatop-reg-offset", + &anatop_reg->control_reg); + if (ret) + return log_msg_ret("anatop-reg-offset", ret); + + ret = dev_read_u32(dev, + "anatop-vol-bit-width", + &anatop_reg->vol_bit_width); + if (ret) + return log_msg_ret("anatop-vol-bit-width", ret); + + ret = dev_read_u32(dev, + "anatop-vol-bit-shift", + &anatop_reg->vol_bit_shift); + if (ret) + return log_msg_ret("anatop-vol-bit-shift", ret); + + ret = dev_read_u32(dev, + "anatop-min-bit-val", + &anatop_reg->min_bit_val); + if (ret) + return log_msg_ret("anatop-min-bit-val", ret); + + ret = dev_read_u32(dev, + "anatop-min-voltage", + &anatop_reg->min_voltage); + if (ret) + return log_msg_ret("anatop-min-voltage", ret); + + ret = dev_read_u32(dev, + "anatop-max-voltage", + &anatop_reg->max_voltage); + if (ret) + return log_msg_ret("anatop-max-voltage", ret); + + /* read LDO ramp up setting, only for core reg */ + dev_read_u32(dev, "anatop-delay-reg-offset", + &anatop_reg->delay_reg); + dev_read_u32(dev, "anatop-delay-bit-width", + &anatop_reg->delay_bit_width); + dev_read_u32(dev, "anatop-delay-bit-shift", + &anatop_reg->delay_bit_shift); + + syscon = dev_get_parent(dev); + if (!syscon) { + dev_dbg(dev, "%s: unable to find syscon device\n", __func__); + return -ENOENT; + } + + anatop_reg->regmap = syscon_get_regmap(syscon); + if (IS_ERR(anatop_reg->regmap)) { + dev_dbg(dev, "%s: unable to find regmap (%ld)\n", __func__, + PTR_ERR(anatop_reg->regmap)); + return -ENOENT; + } + + /* check whether need to care about LDO ramp up speed */ + if (anatop_reg->delay_bit_width) { + /* + * the delay for LDO ramp up time is + * based on the register setting, we need + * to calculate how many steps LDO need to + * ramp up, and how much delay needed. (us) + */ + val = anatop_get_bits(dev, + anatop_reg->delay_reg, + anatop_reg->delay_bit_shift, + anatop_reg->delay_bit_width); + uc_pdata->ramp_delay = (LDO_RAMP_UP_UNIT_IN_CYCLES << val) + / LDO_RAMP_UP_FREQ_IN_MHZ + 1; + } + + return 0; +} + +static const struct udevice_id of_anatop_regulator_match_tbl[] = { + { .compatible = "fsl,anatop-regulator", }, + { /* end */ } +}; + +U_BOOT_DRIVER(anatop_regulator) = { + .name = "anatop_regulator", + .id = UCLASS_REGULATOR, + .ops = &anatop_regulator_ops, + .of_match = of_anatop_regulator_match_tbl, + .plat_auto = sizeof(struct anatop_regulator), + .probe = anatop_regulator_probe, +};