From patchwork Thu Oct 16 11:02:31 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "jinkun.hong" X-Patchwork-Id: 5091481 Return-Path: X-Original-To: patchwork-linux-rockchip@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id E8891C11AC for ; Thu, 16 Oct 2014 11:07:46 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B0FF7201F4 for ; Thu, 16 Oct 2014 11:07:45 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 76E35201ED for ; Thu, 16 Oct 2014 11:07:44 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XeiuF-00074i-JN; Thu, 16 Oct 2014 11:07:43 +0000 Received: from mail-pd0-f194.google.com ([209.85.192.194]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1XeiqH-0002T2-7m; Thu, 16 Oct 2014 11:03:39 +0000 Received: by mail-pd0-f194.google.com with SMTP id y10so1127935pdj.9 for ; Thu, 16 Oct 2014 04:03:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=a6q9WlmI7h/V7TipG4ePMJmdqu0CQTKwdsxGlLWwx4c=; b=WsWEwYkmYyQolqTh3FpG5qV/t69wDP0PrmU/iIFQsYfDZsXUTvYZCSFYXLMuzcFI12 GCrLy+9x9MC94Va+FWGjRAhs4Zpz5wlDQzZbMrJmdaXZnPAWJxqz7Ve2SGYXluxZRBDi YWDukP7D1NfGaYE4xfw0ve88jJvee7snq93dodNfuSuYRchjDVOWB8lrmYjs8Kxy3GRV XmauZkMp9JvuoIw5pmYfd6FMucCK6UG48s7acvBMyC0/+f7/GFIAhmh1vnqvDVD/osX/ cR/1yO6ctK1at404695QBFGXNkc62mvZaI2/AE7R1owQuik+mtVaqVgLT33b09mqPNpp UuzQ== X-Received: by 10.66.66.193 with SMTP id h1mr439609pat.103.1413457393782; Thu, 16 Oct 2014 04:03:13 -0700 (PDT) Received: from ubuntu.localdomain ([58.22.7.114]) by mx.google.com with ESMTPSA id nx6sm19636482pab.46.2014.10.16.04.03.04 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 16 Oct 2014 04:03:13 -0700 (PDT) From: "jinkun.hong" To: linus.walleij@linaro.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v3 1/3] power-domain: add power domain drivers for Rockchip platform Date: Thu, 16 Oct 2014 04:02:31 -0700 Message-Id: <1413457353-4222-2-git-send-email-jinkun.hong@rock-chips.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1413457353-4222-1-git-send-email-jinkun.hong@rock-chips.com> References: <1413457353-4222-1-git-send-email-jinkun.hong@rock-chips.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20141016_040337_400335_4A779297 X-CRM114-Status: GOOD ( 18.35 ) X-Spam-Score: -0.7 (/) Cc: Mark Rutland , devicetree@vger.kernel.org, Ulf Hansson , Russell King , Heiko Stuebner , Pawel Moll , Ian Campbell , "jinkun.hong" , Randy Dunlap , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dianders@chromium.org, linux-rockchip@lists.infradead.org, Rob Herring , Kumar Gala , Grant Likely , Jack Dai X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "Linux-rockchip" Errors-To: linux-rockchip-bounces+patchwork-linux-rockchip=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: "jinkun.hong" Add power domain drivers based on generic power domain for Rockchip platform, and support RK3288. Signed-off-by: Jack Dai Signed-off-by: jinkun.hong --- Changes in v3: - change use pm_clk_resume() and pm_clk_suspend() Changes in v2: - remove the "pd->pd.of_node = np" arch/arm/mach-rockchip/Kconfig | 1 + arch/arm/mach-rockchip/Makefile | 1 + arch/arm/mach-rockchip/pm_domains.c | 329 +++++++++++++++++++++++++++++++++++ 3 files changed, 331 insertions(+) create mode 100644 arch/arm/mach-rockchip/pm_domains.c diff --git a/arch/arm/mach-rockchip/Kconfig b/arch/arm/mach-rockchip/Kconfig index d168669..4920a88 100644 --- a/arch/arm/mach-rockchip/Kconfig +++ b/arch/arm/mach-rockchip/Kconfig @@ -12,6 +12,7 @@ config ARCH_ROCKCHIP select DW_APB_TIMER_OF select ARM_GLOBAL_TIMER select CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK + select PM_GENERIC_DOMAINS if PM help Support for Rockchip's Cortex-A9 Single-to-Quad-Core-SoCs containing the RK2928, RK30xx and RK31xx series. diff --git a/arch/arm/mach-rockchip/Makefile b/arch/arm/mach-rockchip/Makefile index b29d8ea..805268d 100644 --- a/arch/arm/mach-rockchip/Makefile +++ b/arch/arm/mach-rockchip/Makefile @@ -2,3 +2,4 @@ CFLAGS_platsmp.o := -march=armv7-a obj-$(CONFIG_ARCH_ROCKCHIP) += rockchip.o obj-$(CONFIG_SMP) += headsmp.o platsmp.o +obj-$(CONFIG_PM_GENERIC_DOMAINS) += pm_domains.o diff --git a/arch/arm/mach-rockchip/pm_domains.c b/arch/arm/mach-rockchip/pm_domains.c new file mode 100644 index 0000000..1d595f1 --- /dev/null +++ b/arch/arm/mach-rockchip/pm_domains.c @@ -0,0 +1,329 @@ +/* + * Rockchip Generic power domain support. + * + * Copyright (c) 2014 ROCKCHIP, Co. Ltd. + * Author: Hong Jinkun + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PWR_OFFSET 0x08 +#define STATUS_OFFSET 0x0c +#define REQ_OFFSET 0x10 +#define IDLE_OFFSET 0x14 +#define ACK_OFFSET 0x14 + +struct rockchip_domain { + struct generic_pm_domain base; + struct device *dev; + struct regmap *regmap_pmu; + spinlock_t idle_lock; + spinlock_t pmu_lock; + u32 pwr_shift; + u32 status_shift; + u32 req_shift; + u32 idle_shift; + u32 ack_shift; +}; + +#define to_rockchip_pd(_gpd) container_of(_gpd, struct rockchip_domain, base) + +static int rockchip_pmu_set_idle_request(struct rockchip_domain *pd, + bool idle) +{ + u32 idle_mask = BIT(pd->idle_shift); + u32 idle_target = idle << (pd->idle_shift); + u32 ack_mask = BIT(pd->ack_shift); + u32 ack_target = idle << (pd->ack_shift); + unsigned int mask = BIT(pd->req_shift); + unsigned int val; + unsigned long flags; + + spin_lock_irqsave(&pd->idle_lock, flags); + val = (idle) ? mask : 0; + regmap_update_bits(pd->regmap_pmu, REQ_OFFSET, mask, val); + dsb(); + + do { + regmap_read(pd->regmap_pmu, ACK_OFFSET, &val); + } while ((val & ack_mask) != ack_target); + + do { + regmap_read(pd->regmap_pmu, IDLE_OFFSET, &val); + } while ((val & idle_mask) != idle_target); + + spin_unlock_irqrestore(&pd->idle_lock, flags); + + return 0; +} + +static bool rockchip_pmu_power_domain_is_on(struct rockchip_domain *pd) +{ + unsigned int val; + + regmap_read(pd->regmap_pmu, STATUS_OFFSET, &val); + + /* 1'b0: power on, 1'b1: power off */ + return !(val & BIT(pd->status_shift)); +} + +static void rockchip_do_pmu_set_power_domain( + struct rockchip_domain *pd, bool on) +{ + unsigned int mask = BIT(pd->pwr_shift); + unsigned int val; + + val = (on) ? 0 : mask; + regmap_update_bits(pd->regmap_pmu, PWR_OFFSET, mask, val); + dsb(); + + do { + regmap_read(pd->regmap_pmu, STATUS_OFFSET, &val); + } while ((val & BIT(pd->status_shift)) == on); +} + +static int rockchip_pmu_set_power_domain(struct rockchip_domain *pd, + bool on) +{ + unsigned long flags; + + spin_lock_irqsave(&pd->pmu_lock, flags); + if (rockchip_pmu_power_domain_is_on(pd) == on) + goto out; + + if (!on) { + /* FIXME: add code to save AXI_QOS */ + /* if power down, idle request to NIU first */ + rockchip_pmu_set_idle_request(pd, true); + } + + rockchip_do_pmu_set_power_domain(pd, on); + + if (on) { + /* if power up, idle request release to NIU */ + rockchip_pmu_set_idle_request(pd, false); + /* FIXME: add code to restore AXI_QOS */ + } +out: + spin_unlock_irqrestore(&pd->pmu_lock, flags); + return 0; +} + +static int rockchip_pd_power_on(struct generic_pm_domain *domain) +{ + int ret = 0; + struct rockchip_domain *pd = to_rockchip_pd(domain); + + if (pd->dev == NULL) + pr_err("pd->dev is NULL, power on failed!", __func__); + + pm_clk_resume(pd->dev); + ret = rockchip_pmu_set_power_domain(pd, true); + pm_clk_suspend(pd->dev); + + return ret; +} + +static int rockchip_pd_power_off(struct generic_pm_domain *domain) +{ + int ret = 0; + struct rockchip_domain *pd = to_rockchip_pd(domain); + + if (pd->dev == NULL) + pr_err("pd->dev is NULL, power off failed!", __func__); + + pm_clk_resume(pd->dev); + ret = rockchip_pmu_set_power_domain(pd, false); + pm_clk_suspend(pd->dev); + + return 0; +} + +void rockchip_pm_domain_attach_dev(struct device *dev) +{ + struct clk *clk; + int ret; + int i = 0; + struct rockchip_domain *pd; + + pd = (struct rockchip_domain *)dev->pm_domain; + pd->dev = dev; + + ret = pm_clk_create(dev); + if (ret) { + dev_err(dev, "pm_clk_create failed %d\n", ret); + return; + }; + + while ((clk = of_clk_get(dev->of_node, i++)) && !IS_ERR(clk)) { + ret = pm_clk_add_clk(dev, clk); + if (ret) { + dev_err(dev, "pm_clk_add_clk failed %d\n", ret); + goto clk_err; + }; + } + + if (!IS_ENABLED(CONFIG_PM_RUNTIME)) { + ret = pm_clk_resume(dev); + if (ret) { + dev_err(dev, "pm_clk_resume failed %d\n", ret); + goto clk_err; + }; + } + return; + +clk_err: + pm_clk_destroy(dev); + +} + +void rockchip_pm_domain_detach_dev(struct device *dev) +{ + struct rockchip_domain *pd; + + pd = (struct rockchip_domain *)dev->pm_domain; + pd->dev = NULL; + + pm_clk_destroy(dev); +} + +static const struct of_device_id rockchip_pm_domain_dt_match[]; + +static int rockchip_pm_domain_probe(struct platform_device *pdev) +{ + struct rockchip_domain *pd; + const struct of_device_id *match; + struct clk *clk; + int i = 0; + int ret; + struct regmap *regmap_pmu; + struct device_node *node; + + match = of_match_node(rockchip_pm_domain_dt_match, pdev->dev.of_node); + pd = (struct rockchip_domain *)match->data; + + if (!pd) + return -ENOMEM; + + node = of_parse_phandle(pdev->dev.of_node, "rockchip,pmu", 0); + regmap_pmu = syscon_node_to_regmap(node); + of_node_put(node); + if (IS_ERR(regmap_pmu)) { + pr_err("%s: failed to get regmap_pmu", __func__); + return PTR_ERR(regmap_pmu); + } + + pd->regmap_pmu = regmap_pmu; + pd->dev = NULL; + + spin_lock_init(&pd->idle_lock); + spin_lock_init(&pd->pmu_lock); + + pm_genpd_init(&pd->base, NULL, false); + + return of_genpd_add_provider_simple(pdev->dev.of_node, &pd->base); +} + +static struct rockchip_domain gpu_domain = { + .base = { + .name = "pd_gpu", + .attach_dev = rockchip_pm_domain_attach_dev, + .detach_dev = rockchip_pm_domain_detach_dev, + .power_off = rockchip_pd_power_off, + .power_on = rockchip_pd_power_on, + }, + .pwr_shift = 9, + .status_shift = 9, + .req_shift = 2, + .idle_shift = 2, + .ack_shift = 18, +}; + +static struct rockchip_domain hevc_domain = { + .base = { + .name = "pd_hevc", + .attach_dev = rockchip_pm_domain_attach_dev, + .detach_dev = rockchip_pm_domain_detach_dev, + .power_off = rockchip_pd_power_off, + .power_on = rockchip_pd_power_on, + }, + .pwr_shift = 14, + .status_shift = 10, + .req_shift = 9, + .idle_shift = 9, + .ack_shift = 25, + +}; + +static struct rockchip_domain video_domain = { + .base = { + .name = "pd_video", + .attach_dev = rockchip_pm_domain_attach_dev, + .detach_dev = rockchip_pm_domain_detach_dev, + .power_off = rockchip_pd_power_off, + .power_on = rockchip_pd_power_on, + }, + .pwr_shift = 8, + .status_shift = 8, + .req_shift = 3, + .idle_shift = 3, + .ack_shift = 19, +}; + +static struct rockchip_domain vio_domain = { + .base = { + .name = "pd_vio", + .attach_dev = rockchip_pm_domain_attach_dev, + .detach_dev = rockchip_pm_domain_detach_dev, + .power_off = rockchip_pd_power_off, + .power_on = rockchip_pd_power_on, + }, + .pwr_shift = 7, + .status_shift = 7, + .req_shift = 4, + .idle_shift = 4, + .ack_shift = 20, +}; + +static const struct of_device_id rockchip_pm_domain_dt_match[] = { + { .compatible = "rockchip,rk3288-power-gpu", + .data = (void *)&gpu_domain}, + { .compatible = "rockchip,rk3288-power-hevc", + .data = (void *)&hevc_domain}, + { .compatible = "rockchip,rk3288-power-video", + .data = (void *)&video_domain}, + { .compatible = "rockchip,rk3288-power-vio", + .data = (void *)&vio_domain}, + {}, +}; +MODULE_DEVICE_TABLE(of, rockchip_pm_domain_dt_match); + +static struct platform_driver rockchip_pm_domain_driver = { + .probe = rockchip_pm_domain_probe, + .driver = { + .name = "rockchip-pm-domain", + .owner = THIS_MODULE, + .of_match_table = rockchip_pm_domain_dt_match, + }, +}; + +static int __init rockchip_pm_domain_drv_register(void) +{ + return platform_driver_register(&rockchip_pm_domain_driver); +} +postcore_initcall(rockchip_pm_domain_drv_register);