From: Chunyan Zhang <chunyan.zhang@spreadtrum.com>
To: Stephen Boyd <sboyd@codeaurora.org>,
	Michael Turquette <mturquette@baylibre.com>,
	Rob Herring <robh+dt@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will.deacon@arm.com>,
	linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org,
	devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	Arnd Bergmann <arnd@arndb.de>, Mark Brown <broonie@kernel.org>,
	Xiaolong Zhang <xiaolong.zhang@spreadtrum.com>,
	Ben Li <ben.li@spreadtrum.com>,
	Orson Zhai <orson.zhai@spreadtrum.com>,
	Chunyan Zhang <zhang.lyra@gmail.com>
Subject: [PATCH V7 03/12] clk: sprd: add gate clock support
Date: Thu, 7 Dec 2017 20:57:06 +0800	[thread overview]
Message-ID: <20171207125715.16160-4-chunyan.zhang@spreadtrum.com> (raw)
In-Reply-To: <20171207125715.16160-1-chunyan.zhang@spreadtrum.com>
Some clocks on the Spreadtrum's SoCs are just simple gates. Add
support for those clocks.
Signed-off-by: Chunyan Zhang <chunyan.zhang@spreadtrum.com>
---
 drivers/clk/sprd/Makefile |   1 +
 drivers/clk/sprd/gate.c   | 111 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/clk/sprd/gate.h   |  59 ++++++++++++++++++++++++
 3 files changed, 171 insertions(+)
 create mode 100644 drivers/clk/sprd/gate.c
 create mode 100644 drivers/clk/sprd/gate.h
diff --git a/drivers/clk/sprd/Makefile b/drivers/clk/sprd/Makefile
index 74f4b80..8cd5592 100644
--- a/drivers/clk/sprd/Makefile
+++ b/drivers/clk/sprd/Makefile
@@ -1,3 +1,4 @@
 obj-$(CONFIG_SPRD_COMMON_CLK)	+= clk-sprd.o
 
 clk-sprd-y	+= common.o
+clk-sprd-y	+= gate.o
diff --git a/drivers/clk/sprd/gate.c b/drivers/clk/sprd/gate.c
new file mode 100644
index 0000000..f59d193
--- /dev/null
+++ b/drivers/clk/sprd/gate.c
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// Spreadtrum gate clock driver
+//
+// Copyright (C) 2017 Spreadtrum, Inc.
+// Author: Chunyan Zhang <chunyan.zhang@spreadtrum.com>
+
+#include <linux/clk-provider.h>
+#include <linux/regmap.h>
+
+#include "gate.h"
+
+static void clk_gate_toggle(const struct sprd_gate *sg, bool en)
+{
+	const struct sprd_clk_common *common = &sg->common;
+	unsigned int reg;
+	bool set = sg->flags & CLK_GATE_SET_TO_DISABLE ? true : false;
+
+	set ^= en;
+
+	regmap_read(common->regmap, common->reg, ®);
+
+	if (set)
+		reg |= sg->enable_mask;
+	else
+		reg &= ~sg->enable_mask;
+
+	regmap_write(common->regmap, common->reg, reg);
+}
+
+static void clk_sc_gate_toggle(const struct sprd_gate *sg, bool en)
+{
+	const struct sprd_clk_common *common = &sg->common;
+	bool set = sg->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
+	unsigned int offset;
+
+	set ^= en;
+
+	/*
+	 * Each set/clear gate clock has three registers:
+	 * common->reg			- base register
+	 * common->reg + offset		- set register
+	 * common->reg + 2 * offset	- clear register
+	 */
+	offset = set ? sg->sc_offset : sg->sc_offset * 2;
+
+	regmap_write(common->regmap, common->reg + offset,
+			  sg->enable_mask);
+}
+
+static void sprd_gate_disable(struct clk_hw *hw)
+{
+	struct sprd_gate *sg = hw_to_sprd_gate(hw);
+
+	clk_gate_toggle(sg, false);
+}
+
+static int sprd_gate_enable(struct clk_hw *hw)
+{
+	struct sprd_gate *sg = hw_to_sprd_gate(hw);
+
+	clk_gate_toggle(sg, true);
+
+	return 0;
+}
+
+static void sprd_sc_gate_disable(struct clk_hw *hw)
+{
+	struct sprd_gate *sg = hw_to_sprd_gate(hw);
+
+	clk_sc_gate_toggle(sg, false);
+}
+
+static int sprd_sc_gate_enable(struct clk_hw *hw)
+{
+	struct sprd_gate *sg = hw_to_sprd_gate(hw);
+
+	clk_sc_gate_toggle(sg, true);
+
+	return 0;
+}
+static int sprd_gate_is_enabled(struct clk_hw *hw)
+{
+	struct sprd_gate *sg = hw_to_sprd_gate(hw);
+	struct sprd_clk_common *common = &sg->common;
+	unsigned int reg;
+
+	regmap_read(common->regmap, common->reg, ®);
+
+	if (sg->flags & CLK_GATE_SET_TO_DISABLE)
+		reg ^= sg->enable_mask;
+
+	reg &= sg->enable_mask;
+
+	return reg ? 1 : 0;
+}
+
+const struct clk_ops sprd_gate_ops = {
+	.disable	= sprd_gate_disable,
+	.enable		= sprd_gate_enable,
+	.is_enabled	= sprd_gate_is_enabled,
+};
+EXPORT_SYMBOL_GPL(sprd_gate_ops);
+
+const struct clk_ops sprd_sc_gate_ops = {
+	.disable	= sprd_sc_gate_disable,
+	.enable		= sprd_sc_gate_enable,
+	.is_enabled	= sprd_gate_is_enabled,
+};
+EXPORT_SYMBOL_GPL(sprd_sc_gate_ops);
+
diff --git a/drivers/clk/sprd/gate.h b/drivers/clk/sprd/gate.h
new file mode 100644
index 0000000..2e582c6
--- /dev/null
+++ b/drivers/clk/sprd/gate.h
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// Spreadtrum gate clock driver
+//
+// Copyright (C) 2017 Spreadtrum, Inc.
+// Author: Chunyan Zhang <chunyan.zhang@spreadtrum.com>
+
+#ifndef _SPRD_GATE_H_
+#define _SPRD_GATE_H_
+
+#include "common.h"
+
+struct sprd_gate {
+	u32			enable_mask;
+	u16			flags;
+	u16			sc_offset;
+
+	struct sprd_clk_common	common;
+};
+
+#define SPRD_SC_GATE_CLK_OPS(_struct, _name, _parent, _reg, _sc_offset,	\
+			     _enable_mask, _flags, _gate_flags, _ops)	\
+	struct sprd_gate _struct = {					\
+		.enable_mask	= _enable_mask,				\
+		.sc_offset	= _sc_offset,				\
+		.flags		= _gate_flags,				\
+		.common	= {						\
+			.regmap		= NULL,				\
+			.reg		= _reg,				\
+			.hw.init	= CLK_HW_INIT(_name,		\
+						      _parent,		\
+						      _ops,		\
+						      _flags),		\
+		}							\
+	}
+
+#define SPRD_GATE_CLK(_struct, _name, _parent, _reg,			\
+		      _enable_mask, _flags, _gate_flags)		\
+	SPRD_SC_GATE_CLK_OPS(_struct, _name, _parent, _reg, 0,		\
+			     _enable_mask, _flags, _gate_flags,		\
+			     &sprd_gate_ops)
+
+#define SPRD_SC_GATE_CLK(_struct, _name, _parent, _reg, _sc_offset,	\
+			 _enable_mask, _flags, _gate_flags)		\
+	SPRD_SC_GATE_CLK_OPS(_struct, _name, _parent, _reg, _sc_offset,	\
+			     _enable_mask, _flags, _gate_flags,		\
+			     &sprd_sc_gate_ops)
+
+static inline struct sprd_gate *hw_to_sprd_gate(const struct clk_hw *hw)
+{
+	struct sprd_clk_common *common = hw_to_sprd_clk_common(hw);
+
+	return container_of(common, struct sprd_gate, common);
+}
+
+extern const struct clk_ops sprd_gate_ops;
+extern const struct clk_ops sprd_sc_gate_ops;
+
+#endif /* _SPRD_GATE_H_ */
-- 
2.7.4
next prev parent reply	other threads:[~2017-12-07 12:57 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-07 12:57 [PATCH V7 00/12] add clock driver for Spreadtrum platforms Chunyan Zhang
2017-12-07 12:57 ` [PATCH V7 01/12] drivers: move clock common macros out from vendor directories Chunyan Zhang
     [not found]   ` <20171207125715.16160-2-chunyan.zhang-lxIno14LUO0EEoCn2XhGlw@public.gmane.org>
2017-12-21 23:02     ` Stephen Boyd
2017-12-07 12:57 ` [PATCH V7 02/12] clk: sprd: Add common infrastructure Chunyan Zhang
2017-12-07 13:25   ` Philippe Ombredanne
     [not found]   ` <20171207125715.16160-3-chunyan.zhang-lxIno14LUO0EEoCn2XhGlw@public.gmane.org>
2017-12-21 23:02     ` Stephen Boyd
2017-12-07 12:57 ` Chunyan Zhang [this message]
2017-12-21 23:02   ` [PATCH V7 03/12] clk: sprd: add gate clock support Stephen Boyd
2017-12-07 12:57 ` [PATCH V7 04/12] clk: sprd: add mux " Chunyan Zhang
2017-12-21 23:02   ` Stephen Boyd
2017-12-07 12:57 ` [PATCH V7 05/12] clk: sprd: add divider " Chunyan Zhang
2017-12-21 23:02   ` Stephen Boyd
2017-12-07 12:57 ` [PATCH V7 06/12] clk: sprd: add composite " Chunyan Zhang
2017-12-21 23:02   ` Stephen Boyd
2017-12-07 12:57 ` [PATCH V7 07/12] clk: sprd: add adjustable pll support Chunyan Zhang
2017-12-21 23:02   ` Stephen Boyd
2017-12-07 12:57 ` [PATCH V7 08/12] dt-bindings: Add Spreadtrum clock binding documentation Chunyan Zhang
     [not found]   ` <20171207125715.16160-9-chunyan.zhang-lxIno14LUO0EEoCn2XhGlw@public.gmane.org>
2017-12-21 23:02     ` Stephen Boyd
2017-12-07 12:57 ` [PATCH V7 09/12] clk: sprd: Add dt-bindings include file for SC9860 Chunyan Zhang
2017-12-21 23:02   ` Stephen Boyd
2017-12-07 12:57 ` [PATCH V7 10/12] clk: sprd: add clocks support " Chunyan Zhang
2017-12-21 23:03   ` Stephen Boyd
2017-12-07 12:57 ` [PATCH V7 11/12] arm64: dts: add syscon for whale2 platform Chunyan Zhang
     [not found]   ` <20171207125715.16160-12-chunyan.zhang-lxIno14LUO0EEoCn2XhGlw@public.gmane.org>
2017-12-21 23:03     ` Stephen Boyd
     [not found]       ` <20171221230316.GY7997-sgV2jX0FEOL9JmXXK+q4OQ@public.gmane.org>
2017-12-22  5:30         ` Chunyan Zhang
2018-01-04 16:51           ` Arnd Bergmann
2017-12-07 12:57 ` [PATCH V7 12/12] arm64: dts: add clocks for SC9860 Chunyan Zhang
2017-12-22  5:31   ` Chunyan Zhang
2018-01-04 21:34   ` Arnd Bergmann
2018-01-04 23:01     ` Arnd Bergmann
2018-01-05  5:27       ` Chunyan Zhang
2017-12-18  9:46 ` [PATCH V7 00/12] add clock driver for Spreadtrum platforms Chunyan Zhang
     [not found] ` <20171207125715.16160-1-chunyan.zhang-lxIno14LUO0EEoCn2XhGlw@public.gmane.org>
2018-01-04  7:08   ` [RECEND PATCH V7 11/12] arm64: dts: add syscon for whale2 platform Chunyan Zhang
2018-01-04  7:08     ` [RECEND PATCH V7 12/12] arm64: dts: add clocks for SC9860 Chunyan Zhang
     [not found]       ` <1515049684-23481-2-git-send-email-zhang.chunyan-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2018-01-04 16:51         ` Arnd Bergmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox
  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):
  git send-email \
    --in-reply-to=20171207125715.16160-4-chunyan.zhang@spreadtrum.com \
    --to=chunyan.zhang@spreadtrum.com \
    --cc=arnd@arndb.de \
    --cc=ben.li@spreadtrum.com \
    --cc=broonie@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mturquette@baylibre.com \
    --cc=orson.zhai@spreadtrum.com \
    --cc=robh+dt@kernel.org \
    --cc=sboyd@codeaurora.org \
    --cc=will.deacon@arm.com \
    --cc=xiaolong.zhang@spreadtrum.com \
    --cc=zhang.lyra@gmail.com \
    /path/to/YOUR_REPLY
  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
  Be sure your reply has a Subject: header at the top and a blank line
  before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).