From da29636d230df4bfee183979d6a924735d1aaa06 Mon Sep 17 00:00:00 2001
From: Afzal Mohammed <afzal@ti.com>
Date: Thu, 17 Jan 2013 17:11:53 +0530
Subject: [PATCH 06/51] clk: divider: prepare for minimum divider

Some of clocks can have a limit on minimum divider value that can be
programmed, prepare for such a support.

Add a new field min_div for the basic divider clock and a new dynamic
clock divider registration function where minimum divider value can
be specified. Keep behaviour of existing divider clock registration
functions, static initialization helpers as was earlier.

Signed-off-by: Afzal Mohammed <afzal@ti.com>
---
 drivers/clk/clk-divider.c    |   37 ++++++++++++++++++++++++++++++++++---
 include/linux/clk-private.h  |    6 +++++-
 include/linux/clk-provider.h |    7 +++++++
 3 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c
index 5543b7d..da121ac 100644
--- a/drivers/clk/clk-divider.c
+++ b/drivers/clk/clk-divider.c
@@ -251,7 +251,7 @@ EXPORT_SYMBOL_GPL(clk_divider_ops);
 
 static struct clk *_register_divider(struct device *dev, const char *name,
                const char *parent_name, unsigned long flags,
-               void __iomem *reg, u8 shift, u8 width,
+               void __iomem *reg, u8 shift, u8 width, u8 min_div,
                u8 clk_divider_flags, const struct clk_div_table *table,
                spinlock_t *lock)
 {
@@ -266,6 +266,11 @@ static struct clk *_register_divider(struct device *dev, const char *name,
                }
        }
 
+       if (!min_div) {
+               pr_err("%s: minimum divider cannot be zero\n", __func__);
+               return ERR_PTR(-EINVAL);
+       }
+
        /* allocate the divider */
        div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL);
        if (!div) {
@@ -283,6 +288,7 @@ static struct clk *_register_divider(struct device *dev, const char *name,
        div->reg = reg;
        div->shift = shift;
        div->width = width;
+       div->min_div = min_div;
        div->flags = clk_divider_flags;
        div->lock = lock;
        div->hw.init = &init;
@@ -298,6 +304,29 @@ static struct clk *_register_divider(struct device *dev, const char *name,
 }
 
 /**
+ * clk_register_min_divider - register a divider clock having minimum divider
+ * constraints with clock framework
+ * @dev: device registering this clock
+ * @name: name of this clock
+ * @parent_name: name of clock's parent
+ * @flags: framework-specific flags
+ * @reg: register address to adjust divider
+ * @shift: number of bits to shift the bitfield
+ * @width: width of the bitfield
+ * @min_div: minimum allowable divider
+ * @clk_divider_flags: divider-specific flags for this clock
+ * @lock: shared register lock for this clock
+ */
+struct clk *clk_register_min_divider(struct device *dev, const char *name,
+               const char *parent_name, unsigned long flags,
+               void __iomem *reg, u8 shift, u8 width, u8 min_div,
+               u8 clk_divider_flags, spinlock_t *lock)
+{
+       return _register_divider(dev, name, parent_name, flags, reg, shift,
+                       width, min_div, clk_divider_flags, NULL, lock);
+}
+
+/**
  * clk_register_divider - register a divider clock with the clock framework
  * @dev: device registering this clock
  * @name: name of this clock
@@ -315,7 +344,8 @@ struct clk *clk_register_divider(struct device *dev, const char *name,
                u8 clk_divider_flags, spinlock_t *lock)
 {
        return _register_divider(dev, name, parent_name, flags, reg, shift,
-                       width, clk_divider_flags, NULL, lock);
+                       width, CLK_DIVIDER_MIN_DIV_DEFAULT, clk_divider_flags,
+                       NULL, lock);
 }
 EXPORT_SYMBOL_GPL(clk_register_divider);
 
@@ -340,6 +370,7 @@ struct clk *clk_register_divider_table(struct device *dev, const char *name,
                spinlock_t *lock)
 {
        return _register_divider(dev, name, parent_name, flags, reg, shift,
-                       width, clk_divider_flags, table, lock);
+                       width, CLK_DIVIDER_MIN_DIV_DEFAULT, clk_divider_flags,
+                       table, lock);
 }
 EXPORT_SYMBOL_GPL(clk_register_divider_table);
diff --git a/include/linux/clk-private.h b/include/linux/clk-private.h
index 8138c94..c20dd74 100644
--- a/include/linux/clk-private.h
+++ b/include/linux/clk-private.h
@@ -108,7 +108,8 @@ struct clk {
 
 #define _DEFINE_CLK_DIVIDER(_name, _parent_name, _parent_ptr,  \
                                _flags, _reg, _shift, _width,   \
-                               _divider_flags, _table, _lock)  \
+                               _min_div, _divider_flags,       \
+                               _table, _lock)                  \
        static struct clk _name;                                \
        static const char *_name##_parent_names[] = {           \
                _parent_name,                                   \
@@ -123,6 +124,7 @@ struct clk {
                .reg = _reg,                                    \
                .shift = _shift,                                \
                .width = _width,                                \
+               .min_div = _min_div,                            \
                .flags = _divider_flags,                        \
                .table = _table,                                \
                .lock = _lock,                                  \
@@ -135,6 +137,7 @@ struct clk {
                                _divider_flags, _lock)          \
        _DEFINE_CLK_DIVIDER(_name, _parent_name, _parent_ptr,   \
                                _flags, _reg, _shift, _width,   \
+                               CLK_DIVIDER_MIN_DIV_DEFAULT,    \
                                _divider_flags, NULL, _lock)
 
 #define DEFINE_CLK_DIVIDER_TABLE(_name, _parent_name,          \
@@ -143,6 +146,7 @@ struct clk {
                                _table, _lock)                  \
        _DEFINE_CLK_DIVIDER(_name, _parent_name, _parent_ptr,   \
                                _flags, _reg, _shift, _width,   \
+                               CLK_DIVIDER_MIN_DIV_DEFAULT,    \
                                _divider_flags, _table, _lock)  \
 
 #define DEFINE_CLK_MUX(_name, _parent_names, _parents, _flags, \
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index 73bdb69..c44a8e2 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -281,17 +281,24 @@ struct clk_divider {
        void __iomem    *reg;
        u8              shift;
        u8              width;
+       u8              min_div;
        u8              flags;
        const struct clk_div_table      *table;
        spinlock_t      *lock;
 };
 
+#define        CLK_DIVIDER_MIN_DIV_DEFAULT     1
+
 #define CLK_DIVIDER_ONE_BASED          BIT(0)
 #define CLK_DIVIDER_POWER_OF_TWO       BIT(1)
 #define CLK_DIVIDER_ALLOW_ZERO         BIT(2)
 #define CLK_DIVIDER_HIWORD_MASK                BIT(3)
 
 extern const struct clk_ops clk_divider_ops;
+struct clk *clk_register_min_divider(struct device *dev, const char *name,
+               const char *parent_name, unsigned long flags,
+               void __iomem *reg, u8 shift, u8 width, u8 min_div,
+               u8 clk_divider_flags, spinlock_t *lock);
 struct clk *clk_register_divider(struct device *dev, const char *name,
                const char *parent_name, unsigned long flags,
                void __iomem *reg, u8 shift, u8 width,
-- 
1.7.10.4