[PATCH v5 16/20] clk: add support for clocks provided by SCMI

2018-02-12 Thread Sudeep Holla
On some ARM based systems, a separate Cortex-M based System Control
Processor(SCP) provides the overall power, clock, reset and system
control. System Control and Management Interface(SCMI) Message Protocol
is defined for the communication between the Application Cores(AP)
and the SCP.

This patch adds support for the clocks provided by SCP using SCMI
protocol.

Cc: linux-...@vger.kernel.org
Cc: Michael Turquette 
Acked-by: Stephen Boyd 
Signed-off-by: Sudeep Holla 
---
 MAINTAINERS|   2 +-
 drivers/clk/Kconfig|  10 +++
 drivers/clk/Makefile   |   1 +
 drivers/clk/clk-scmi.c | 213 +
 4 files changed, 225 insertions(+), 1 deletion(-)
 create mode 100644 drivers/clk/clk-scmi.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 7cede6e7dfed..d6e9e3eae5ac 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13384,7 +13384,7 @@ M:  Sudeep Holla 
 L: linux-arm-ker...@lists.infradead.org
 S: Maintained
 F: Documentation/devicetree/bindings/arm/arm,sc[mp]i.txt
-F: drivers/clk/clk-scpi.c
+F: drivers/clk/clk-sc[mp]i.c
 F: drivers/cpufreq/scpi-cpufreq.c
 F: drivers/firmware/arm_scpi.c
 F: drivers/firmware/arm_scmi/
diff --git a/drivers/clk/Kconfig b/drivers/clk/Kconfig
index 98ce9fc6e6c0..7ae23b25b406 100644
--- a/drivers/clk/Kconfig
+++ b/drivers/clk/Kconfig
@@ -62,6 +62,16 @@ config COMMON_CLK_HI655X
  multi-function device has one fixed-rate oscillator, clocked
  at 32KHz.
 
+config COMMON_CLK_SCMI
+   tristate "Clock driver controlled via SCMI interface"
+   depends on ARM_SCMI_PROTOCOL || COMPILE_TEST
+ ---help---
+ This driver provides support for clocks that are controlled
+ by firmware that implements the SCMI interface.
+
+ This driver uses SCMI Message Protocol to interact with the
+ firmware providing all the clock controls.
+
 config COMMON_CLK_SCPI
tristate "Clock driver controlled via SCPI interface"
depends on ARM_SCPI_PROTOCOL || COMPILE_TEST
diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile
index 71ec41e6364f..6605513eaa94 100644
--- a/drivers/clk/Makefile
+++ b/drivers/clk/Makefile
@@ -41,6 +41,7 @@ obj-$(CONFIG_CLK_QORIQ)   += clk-qoriq.o
 obj-$(CONFIG_COMMON_CLK_RK808) += clk-rk808.o
 obj-$(CONFIG_COMMON_CLK_HI655X)+= clk-hi655x.o
 obj-$(CONFIG_COMMON_CLK_S2MPS11)   += clk-s2mps11.o
+obj-$(CONFIG_COMMON_CLK_SCMI)   += clk-scmi.o
 obj-$(CONFIG_COMMON_CLK_SCPI)   += clk-scpi.o
 obj-$(CONFIG_COMMON_CLK_SI5351)+= clk-si5351.o
 obj-$(CONFIG_COMMON_CLK_SI514) += clk-si514.o
diff --git a/drivers/clk/clk-scmi.c b/drivers/clk/clk-scmi.c
new file mode 100644
index ..1e4d7a57779b
--- /dev/null
+++ b/drivers/clk/clk-scmi.c
@@ -0,0 +1,213 @@
+/*
+ * System Control and Power Interface (SCMI) Protocol based clock driver
+ *
+ * Copyright (C) 2017 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see .
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct scmi_clk {
+   u32 id;
+   struct clk_hw hw;
+   const struct scmi_clock_info *info;
+   const struct scmi_handle *handle;
+};
+
+#define to_scmi_clk(clk) container_of(clk, struct scmi_clk, hw)
+
+static unsigned long scmi_clk_recalc_rate(struct clk_hw *hw,
+ unsigned long parent_rate)
+{
+   int ret;
+   u64 rate;
+   struct scmi_clk *clk = to_scmi_clk(hw);
+
+   ret = clk->handle->clk_ops->rate_get(clk->handle, clk->id, &rate);
+   if (ret)
+   return 0;
+   return rate;
+}
+
+static long scmi_clk_round_rate(struct clk_hw *hw, unsigned long rate,
+   unsigned long *parent_rate)
+{
+   int step;
+   u64 fmin, fmax, ftmp;
+   struct scmi_clk *clk = to_scmi_clk(hw);
+
+   /*
+* We can't figure out what rate it will be, so just return the
+* rate back to the caller. scmi_clk_recalc_rate() will be called
+* after the rate is set and we'll know what rate the clock is
+* running at then.
+*/
+   if (clk->info->rate_discrete)
+   return rate;
+
+   fmin = clk->info->range.min_rate;
+   fmax = clk->info->range.max_rate;
+   if (rate <= fmin)
+   return fmin;
+   else if (rate >= fmax)
+ 

[PATCH v5 16/20] clk: add support for clocks provided by SCMI

2018-01-02 Thread Sudeep Holla
On some ARM based systems, a separate Cortex-M based System Control
Processor(SCP) provides the overall power, clock, reset and system
control. System Control and Management Interface(SCMI) Message Protocol
is defined for the communication between the Application Cores(AP)
and the SCP.

This patch adds support for the clocks provided by SCP using SCMI
protocol.

Cc: linux-...@vger.kernel.org
Cc: Michael Turquette 
Acked-by: Stephen Boyd 
Signed-off-by: Sudeep Holla 
---
 MAINTAINERS|   2 +-
 drivers/clk/Kconfig|  10 +++
 drivers/clk/Makefile   |   1 +
 drivers/clk/clk-scmi.c | 213 +
 4 files changed, 225 insertions(+), 1 deletion(-)
 create mode 100644 drivers/clk/clk-scmi.c

diff --git a/MAINTAINERS b/MAINTAINERS
index e7526be3a05b..d961de6aa0fd 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13154,7 +13154,7 @@ M:  Sudeep Holla 
 L: linux-arm-ker...@lists.infradead.org
 S: Maintained
 F: Documentation/devicetree/bindings/arm/arm,sc[mp]i.txt
-F: drivers/clk/clk-scpi.c
+F: drivers/clk/clk-sc[mp]i.c
 F: drivers/cpufreq/scpi-cpufreq.c
 F: drivers/firmware/arm_scpi.c
 F: drivers/firmware/arm_scmi/
diff --git a/drivers/clk/Kconfig b/drivers/clk/Kconfig
index 1c4e1aa6767e..57c66b22eab8 100644
--- a/drivers/clk/Kconfig
+++ b/drivers/clk/Kconfig
@@ -62,6 +62,16 @@ config COMMON_CLK_HI655X
  multi-function device has one fixed-rate oscillator, clocked
  at 32KHz.
 
+config COMMON_CLK_SCMI
+   tristate "Clock driver controlled via SCMI interface"
+   depends on ARM_SCMI_PROTOCOL || COMPILE_TEST
+ ---help---
+ This driver provides support for clocks that are controlled
+ by firmware that implements the SCMI interface.
+
+ This driver uses SCMI Message Protocol to interact with the
+ firmware providing all the clock controls.
+
 config COMMON_CLK_SCPI
tristate "Clock driver controlled via SCPI interface"
depends on ARM_SCPI_PROTOCOL || COMPILE_TEST
diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile
index f7f761b02bed..da622c01526c 100644
--- a/drivers/clk/Makefile
+++ b/drivers/clk/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_CLK_QORIQ)   += clk-qoriq.o
 obj-$(CONFIG_COMMON_CLK_RK808) += clk-rk808.o
 obj-$(CONFIG_COMMON_CLK_HI655X)+= clk-hi655x.o
 obj-$(CONFIG_COMMON_CLK_S2MPS11)   += clk-s2mps11.o
+obj-$(CONFIG_COMMON_CLK_SCMI)   += clk-scmi.o
 obj-$(CONFIG_COMMON_CLK_SCPI)   += clk-scpi.o
 obj-$(CONFIG_COMMON_CLK_SI5351)+= clk-si5351.o
 obj-$(CONFIG_COMMON_CLK_SI514) += clk-si514.o
diff --git a/drivers/clk/clk-scmi.c b/drivers/clk/clk-scmi.c
new file mode 100644
index ..1e4d7a57779b
--- /dev/null
+++ b/drivers/clk/clk-scmi.c
@@ -0,0 +1,213 @@
+/*
+ * System Control and Power Interface (SCMI) Protocol based clock driver
+ *
+ * Copyright (C) 2017 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see .
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct scmi_clk {
+   u32 id;
+   struct clk_hw hw;
+   const struct scmi_clock_info *info;
+   const struct scmi_handle *handle;
+};
+
+#define to_scmi_clk(clk) container_of(clk, struct scmi_clk, hw)
+
+static unsigned long scmi_clk_recalc_rate(struct clk_hw *hw,
+ unsigned long parent_rate)
+{
+   int ret;
+   u64 rate;
+   struct scmi_clk *clk = to_scmi_clk(hw);
+
+   ret = clk->handle->clk_ops->rate_get(clk->handle, clk->id, &rate);
+   if (ret)
+   return 0;
+   return rate;
+}
+
+static long scmi_clk_round_rate(struct clk_hw *hw, unsigned long rate,
+   unsigned long *parent_rate)
+{
+   int step;
+   u64 fmin, fmax, ftmp;
+   struct scmi_clk *clk = to_scmi_clk(hw);
+
+   /*
+* We can't figure out what rate it will be, so just return the
+* rate back to the caller. scmi_clk_recalc_rate() will be called
+* after the rate is set and we'll know what rate the clock is
+* running at then.
+*/
+   if (clk->info->rate_discrete)
+   return rate;
+
+   fmin = clk->info->range.min_rate;
+   fmax = clk->info->range.max_rate;
+   if (rate <= fmin)
+   return fmin;
+   else if (rate >= fmax)
+