linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tomeu Vizoso <tomeu.vizoso@collabora.com>
To: linux-kernel@vger.kernel.org,
	Mike Turquette <mturquette@linaro.org>,
	Stephen Boyd <sboyd@codeaurora.org>
Cc: Javier Martinez Canillas <javier.martinez@collabora.co.uk>,
	Tomeu Vizoso <tomeu.vizoso@collabora.com>
Subject: [PATCH v13 6/6] clk: Add module for unit tests
Date: Fri, 23 Jan 2015 12:03:33 +0100	[thread overview]
Message-ID: <1422011024-32283-7-git-send-email-tomeu.vizoso@collabora.com> (raw)
In-Reply-To: <1422011024-32283-1-git-send-email-tomeu.vizoso@collabora.com>

This module registers a clock hierarchy and performs several operations
against them checking that the result is expected.

Signed-off-by: Tomeu Vizoso <tomeu.vizoso@collabora.com>
---
 drivers/clk/Kconfig       |   1 +
 drivers/clk/Kconfig.debug |   6 +
 drivers/clk/Makefile      |   1 +
 drivers/clk/clk-test.c    | 326 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 334 insertions(+)
 create mode 100644 drivers/clk/Kconfig.debug
 create mode 100644 drivers/clk/clk-test.c

diff --git a/drivers/clk/Kconfig b/drivers/clk/Kconfig
index 3f44f29..03c500f 100644
--- a/drivers/clk/Kconfig
+++ b/drivers/clk/Kconfig
@@ -135,6 +135,7 @@ config COMMON_CLK_PXA
 	  Sypport for the Marvell PXA SoC.
 
 source "drivers/clk/qcom/Kconfig"
+source "drivers/clk/Kconfig.debug"
 
 endmenu
 
diff --git a/drivers/clk/Kconfig.debug b/drivers/clk/Kconfig.debug
new file mode 100644
index 0000000..840b790
--- /dev/null
+++ b/drivers/clk/Kconfig.debug
@@ -0,0 +1,6 @@
+config COMMON_CLK_TEST
+	tristate "Unit tests for the Common Clock Framework"
+	default n
+	---help---
+	  This driver runs several tests on the Common Clock Framework.
+
diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile
index d5fba5b..4ee1475 100644
--- a/drivers/clk/Makefile
+++ b/drivers/clk/Makefile
@@ -68,3 +68,4 @@ obj-$(CONFIG_ARCH_U8500)		+= ux500/
 obj-$(CONFIG_COMMON_CLK_VERSATILE)	+= versatile/
 obj-$(CONFIG_X86)			+= x86/
 obj-$(CONFIG_ARCH_ZYNQ)			+= zynq/
+obj-$(CONFIG_COMMON_CLK_TEST)		+= clk-test.o
diff --git a/drivers/clk/clk-test.c b/drivers/clk/clk-test.c
new file mode 100644
index 0000000..e1032f2a
--- /dev/null
+++ b/drivers/clk/clk-test.c
@@ -0,0 +1,326 @@
+/*
+ * Copyright (C) 2015 Google, Inc
+ *
+ * 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.
+ *
+ * Unit tests for the Common Clock Framework
+ */
+
+#include <linux/clk-provider.h>
+#include <linux/clkdev.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/printk.h>
+#include <linux/slab.h>
+
+/* Assumed to be sorted */
+static const unsigned long allowed_rates[] = { 0, 100, 200, 300, 400, 500 };
+
+struct test_clk {
+	struct clk_hw hw;
+	unsigned long rate;
+};
+
+static inline struct test_clk *to_test_clk(struct clk_hw *hw)
+{
+	return container_of(hw, struct test_clk, hw);
+}
+
+static long test_clk_determine_rate(struct clk_hw *hw,
+				    unsigned long rate,
+				    unsigned long min_rate,
+				    unsigned long max_rate,
+				    unsigned long *best_parent_rate,
+				    struct clk_hw **best_parent)
+{
+	struct clk *parent;
+	unsigned long target_rate = 0;
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(allowed_rates); i++) {
+
+		if (allowed_rates[i] > max_rate) {
+			if (i > 0)
+				target_rate = allowed_rates[i - 1];
+			else
+				target_rate = 0;
+			break;
+		}
+
+		if (allowed_rates[i] < min_rate)
+			continue;
+
+		if (allowed_rates[i] >= rate) {
+			target_rate = allowed_rates[i];
+			break;
+		}
+	}
+
+	parent = clk_get_parent(hw->clk);
+	if (parent) {
+		*best_parent = __clk_get_hw(parent);
+		*best_parent_rate = __clk_determine_rate(__clk_get_hw(parent),
+							 target_rate / 2,
+							 min_rate,
+							 max_rate);
+	}
+
+	return target_rate;
+}
+
+static unsigned long test_clk_recalc_rate(struct clk_hw *hw,
+					  unsigned long parent_rate)
+{
+	struct test_clk *test_clk = to_test_clk(hw);
+
+	return test_clk->rate;
+}
+
+static int test_clk_set_rate(struct clk_hw *hw, unsigned long rate,
+			     unsigned long parent_rate)
+{
+	struct test_clk *test_clk = to_test_clk(hw);
+
+	test_clk->rate = rate;
+
+	return 0;
+}
+
+static const struct clk_ops test_clk_ops = {
+	.determine_rate = test_clk_determine_rate,
+	.recalc_rate = test_clk_recalc_rate,
+	.set_rate = test_clk_set_rate,
+};
+
+static struct clk *init_test_clk(const char *name, const char *parent_name)
+{
+	struct test_clk *test_clk;
+	struct clk *clk;
+	struct clk_init_data init;
+	int err;
+
+	test_clk = kzalloc(sizeof(*test_clk), GFP_KERNEL);
+	if (!test_clk)
+		return ERR_PTR(-ENOMEM);
+
+	test_clk->rate = 0;
+
+	init.name = name;
+	init.ops = &test_clk_ops;
+
+	if (parent_name) {
+		init.parent_names = &parent_name;
+		init.num_parents = 1;
+		init.flags = CLK_SET_RATE_PARENT;
+	} else {
+		init.parent_names = NULL;
+		init.num_parents = 0;
+		init.flags = CLK_IS_ROOT;
+	}
+
+	test_clk->hw.init = &init;
+
+	clk = clk_register(NULL, &test_clk->hw);
+	if (IS_ERR(clk)) {
+		printk("%s: error registering clk: %ld\n", __func__,
+		       PTR_ERR(clk));
+		return clk;
+	}
+
+	err = clk_register_clkdev(clk, name, NULL);
+	if (err)
+		printk("%s: error registering alias: %d\n", __func__, err);
+
+	return clk;
+}
+
+static void test_ceiling(struct clk *clk)
+{
+	unsigned long rate;
+	int ret;
+
+	ret = clk_set_max_rate(clk, 399);
+	if (ret)
+		printk("%s: error setting ceiling: %d\n", __func__, ret);
+
+	rate = clk_round_rate(clk, 400);
+	if (rate != 300)
+		printk("%s: unexpected rounded rate: %lu != 300\n", __func__, rate);
+
+	ret = clk_set_rate(clk, 400);
+	if (ret)
+		printk("%s: error setting rate: %d\n", __func__, ret);
+
+	rate = clk_get_rate(clk);
+	if (rate != 300)
+		printk("%s: unexpected rate: %lu != 300\n", __func__, rate);
+
+	ret = clk_set_max_rate(clk, ULONG_MAX);
+	if (ret)
+		printk("%s: error setting ceiling: %d\n", __func__, ret);
+}
+
+static void test_floor(struct clk *clk)
+{
+	unsigned long rate;
+	int ret;
+
+	ret = clk_set_min_rate(clk, 199);
+	if (ret)
+		printk("%s: error setting floor: %d\n", __func__, ret);
+
+	rate = clk_round_rate(clk, 90);
+	if (rate != 200)
+		printk("%s: unexpected rounded rate: %lu != 200\n", __func__, rate);
+
+	ret = clk_set_rate(clk, 90);
+	if (ret)
+		printk("%s: error setting rate: %d\n", __func__, ret);
+
+	rate = clk_get_rate(clk);
+	if (rate != 200)
+		printk("%s: unexpected rate: %lu != 200\n", __func__, rate);
+
+	ret = clk_set_min_rate(clk, 0);
+	if (ret)
+		printk("%s: error setting floor: %d\n", __func__, ret);
+}
+
+static void test_unsatisfiable(struct clk *clk)
+{
+	struct clk *clk2 = clk_get_sys(NULL, "clk");
+	unsigned long rate;
+	int ret;
+
+	if (IS_ERR(clk2))
+		printk("%s: error getting clk: %ld\n", __func__,
+		       PTR_ERR(clk2));
+
+	ret = clk_set_min_rate(clk, 99);
+	if (ret)
+		printk("%s: error setting floor: %d\n", __func__, ret);
+
+	ret = clk_set_max_rate(clk, 199);
+	if (ret)
+		printk("%s: error setting ceiling: %d\n", __func__, ret);
+
+	ret = clk_set_min_rate(clk2, 399);
+	if (ret)
+		printk("%s: error setting floor: %d\n", __func__, ret);
+
+	ret = clk_set_max_rate(clk2, 499);
+	if (ret)
+		printk("%s: error setting ceiling: %d\n", __func__, ret);
+
+	ret = clk_set_rate(clk, 90);
+	if (ret)
+		printk("%s: error setting rate: %d\n", __func__, ret);
+
+	/*
+	 * It's expected that the rate is the highest rate that is still
+	 * below the smallest ceiling
+	 */
+	rate = clk_get_rate(clk);
+	if (rate != 100)
+		printk("%s: unexpected rate: %lu != 100\n", __func__, rate);
+
+	clk_put(clk2);
+
+	ret = clk_set_min_rate(clk, 0);
+	if (ret)
+		printk("%s: error setting floor: %d\n", __func__, ret);
+
+	ret = clk_set_max_rate(clk, ULONG_MAX);
+	if (ret)
+		printk("%s: error setting ceiling: %d\n", __func__, ret);
+}
+
+static void test_constrained_parent(struct clk *clk, struct clk *parent)
+{
+	unsigned long rate;
+	int ret;
+
+	ret = clk_set_max_rate(parent, 199);
+	if (ret)
+		printk("%s: error setting ceiling: %d\n", __func__, ret);
+
+	ret = clk_set_rate(clk, 200);
+	if (ret)
+		printk("%s: error setting rate: %d\n", __func__, ret);
+
+	rate = clk_get_rate(clk);
+	if (rate != 200)
+		printk("%s: unexpected rate: %lu != 200\n", __func__, rate);
+
+	rate = clk_get_rate(parent);
+	if (rate != 100)
+		printk("%s: unexpected parent rate: %lu != 100\n", __func__, rate);
+
+	ret = clk_set_max_rate(parent, ULONG_MAX);
+	if (ret)
+		printk("%s: error setting ceiling: %d\n", __func__, ret);
+}
+
+static void test_constraint_with_parent(struct clk *clk, struct clk *parent)
+{
+	unsigned long rate;
+	int ret;
+
+	ret = clk_set_min_rate(clk, 201);
+	if (ret)
+		printk("%s: error setting ceiling: %d\n", __func__, ret);
+
+	ret = clk_set_rate(clk, 300);
+	if (ret)
+		printk("%s: error setting rate: %d\n", __func__, ret);
+
+	rate = clk_get_rate(clk);
+	if (rate != 300)
+		printk("%s: unexpected rate: %lu != 300\n", __func__, rate);
+
+	rate = clk_get_rate(parent);
+	if (rate != 300)
+		printk("%s: unexpected parent rate: %lu != 300\n", __func__, rate);
+
+	ret = clk_set_max_rate(parent, ULONG_MAX);
+	if (ret)
+		printk("%s: error setting ceiling: %d\n", __func__, ret);
+}
+
+static int __init clk_test_init(void)
+{
+	struct clk *parent, *clk;
+
+	printk("---------- Common Clock Framework test results ----------\n");
+
+	parent = init_test_clk("parent", NULL);
+	if (IS_ERR(parent)) {
+		printk("%s: error registering parent: %ld\n", __func__,
+		       PTR_ERR(parent));
+		return PTR_ERR(parent);
+	}
+
+	clk = init_test_clk("clk", "parent");
+	if (IS_ERR(clk)) {
+		printk("%s: error registering clk: %ld\n", __func__,
+		       PTR_ERR(clk));
+		return PTR_ERR(clk);
+	}
+
+	test_ceiling(clk);
+	test_floor(clk);
+	test_unsatisfiable(clk);
+	test_constrained_parent(clk, parent);
+	test_constraint_with_parent(clk, parent);
+
+	printk("---------------------------------------------------------\n");
+
+	return 0;
+}
+
+module_init(clk_test_init);
+
+MODULE_LICENSE("GPL");
-- 
1.9.3


  parent reply	other threads:[~2015-01-23 11:04 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-23 11:03 [PATCH v13 0/6] Per-user clock constraints Tomeu Vizoso
2015-01-23 11:03 ` [PATCH v13 1/6] clk: Remove unneeded NULL checks Tomeu Vizoso
2015-01-23 11:03 ` [PATCH v13 2/6] clk: Remove __clk_register Tomeu Vizoso
2015-01-23 11:03 ` [PATCH v13 3/6] clk: Make clk API return per-user struct clk instances Tomeu Vizoso
2015-02-01 21:24   ` Mike Turquette
2015-02-02 17:04     ` Tony Lindgren
2015-02-02 19:32     ` Tero Kristo
2015-02-02 20:44       ` Tony Lindgren
2015-02-02 22:48         ` Mike Turquette
2015-02-02 23:11           ` Tony Lindgren
2015-02-02 22:41       ` Mike Turquette
2015-02-02 22:52         ` Stephen Boyd
2015-02-03  7:03         ` Tomeu Vizoso
2015-02-03  8:46           ` Tero Kristo
2015-02-03 15:22             ` Tony Lindgren
2015-02-02 20:45     ` Stephen Boyd
2015-02-02 21:31       ` Julia Lawall
2015-02-02 22:35         ` Stephen Boyd
2015-02-02 22:50           ` Mike Turquette
2015-02-03 16:04             ` [Cocci] " Quentin Lambert
2015-02-04 23:26               ` Stephen Boyd
2015-02-05 15:45                 ` Quentin Lambert
2015-02-05 16:02                   ` Quentin Lambert
2015-02-06  1:49                     ` Stephen Boyd
2015-02-06  2:15                   ` Stephen Boyd
2015-02-06  9:01                     ` Quentin Lambert
2015-02-06  9:12                       ` Julia Lawall
2015-02-06 17:15                         ` Stephen Boyd
2015-02-17 22:01                     ` Stephen Boyd
2015-03-12 17:20                       ` Sebastian Andrzej Siewior
2015-03-12 19:43                         ` Stephen Boyd
2015-03-13  3:29                           ` Shawn Guo
2015-03-13  8:20                             ` Sebastian Andrzej Siewior
2015-03-13 13:42                               ` Shawn Guo
2015-03-13 17:42                             ` Stephen Boyd
2015-02-05 19:44   ` Sylwester Nawrocki
2015-02-05 20:06     ` Sylwester Nawrocki
2015-02-05 20:07     ` Stephen Boyd
2015-02-05 22:14       ` Stephen Boyd
2015-02-06  0:42         ` Russell King - ARM Linux
2015-02-06  1:35           ` Stephen Boyd
2015-02-06 13:39             ` Russell King - ARM Linux
2015-02-06 19:30               ` Stephen Boyd
2015-02-06 19:37                 ` Russell King - ARM Linux
2015-02-06 19:41                   ` Stephen Boyd
2015-02-19 21:32                 ` Mike Turquette
2015-02-24 14:08                   ` Russell King - ARM Linux
2015-01-23 11:03 ` [PATCH v13 4/6] clk: Add rate constraints to clocks Tomeu Vizoso
2015-01-29 13:31   ` Geert Uytterhoeven
2015-01-29 19:13     ` Stephen Boyd
2015-01-31  1:31       ` Stephen Boyd
2015-01-31 18:36         ` Tomeu Vizoso
2015-02-01 22:18           ` Mike Turquette
2015-02-02  7:59             ` Geert Uytterhoeven
2015-02-02 16:12               ` Tony Lindgren
2015-02-02 17:46                 ` Mike Turquette
2015-02-02 17:49                   ` Russell King - ARM Linux
2015-02-02 19:21                   ` Tony Lindgren
2015-02-02 20:47                     ` Tony Lindgren
2015-01-23 11:03 ` [PATCH v13 5/6] clkdev: Export clk_register_clkdev Tomeu Vizoso
2015-02-03 17:35   ` Andy Shevchenko
2015-02-03 17:43     ` Andy Shevchenko
2015-01-23 11:03 ` Tomeu Vizoso [this message]
2015-01-27  0:55 ` [PATCH v13 0/6] Per-user clock constraints Stephen Boyd
2015-01-27  6:29   ` Tomeu Vizoso
2015-01-28  6:59   ` Tomeu Vizoso
     [not found]     ` <20150129022633.22722.78592@quantum>
2015-01-29  6:41       ` Tomeu Vizoso
2015-01-29 14:29         ` Mike Turquette

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=1422011024-32283-7-git-send-email-tomeu.vizoso@collabora.com \
    --to=tomeu.vizoso@collabora.com \
    --cc=javier.martinez@collabora.co.uk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mturquette@linaro.org \
    --cc=sboyd@codeaurora.org \
    /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).