Thomas Gleixner | fcaf203 | 2019-05-27 08:55:08 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Liu Ying | a49e6c4 | 2013-07-04 17:35:46 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Freescale Semiconductor, Inc. |
Liu Ying | a49e6c4 | 2013-07-04 17:35:46 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/clk-provider.h> |
| 7 | #include <linux/err.h> |
| 8 | #include <linux/io.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include "clk.h" |
| 11 | |
Liu Ying | a49e6c4 | 2013-07-04 17:35:46 +0800 | [diff] [blame] | 12 | /** |
| 13 | * struct clk_fixup_mux - imx integer fixup multiplexer clock |
| 14 | * @mux: the parent class |
| 15 | * @ops: pointer to clk_ops of parent class |
| 16 | * @fixup: a hook to fixup the write value |
| 17 | * |
| 18 | * The imx fixup multiplexer clock is a subclass of basic clk_mux |
| 19 | * with an addtional fixup hook. |
| 20 | */ |
| 21 | struct clk_fixup_mux { |
| 22 | struct clk_mux mux; |
| 23 | const struct clk_ops *ops; |
| 24 | void (*fixup)(u32 *val); |
| 25 | }; |
| 26 | |
| 27 | static inline struct clk_fixup_mux *to_clk_fixup_mux(struct clk_hw *hw) |
| 28 | { |
| 29 | struct clk_mux *mux = to_clk_mux(hw); |
| 30 | |
| 31 | return container_of(mux, struct clk_fixup_mux, mux); |
| 32 | } |
| 33 | |
| 34 | static u8 clk_fixup_mux_get_parent(struct clk_hw *hw) |
| 35 | { |
| 36 | struct clk_fixup_mux *fixup_mux = to_clk_fixup_mux(hw); |
| 37 | |
| 38 | return fixup_mux->ops->get_parent(&fixup_mux->mux.hw); |
| 39 | } |
| 40 | |
| 41 | static int clk_fixup_mux_set_parent(struct clk_hw *hw, u8 index) |
| 42 | { |
| 43 | struct clk_fixup_mux *fixup_mux = to_clk_fixup_mux(hw); |
| 44 | struct clk_mux *mux = to_clk_mux(hw); |
| 45 | unsigned long flags = 0; |
| 46 | u32 val; |
| 47 | |
| 48 | spin_lock_irqsave(mux->lock, flags); |
| 49 | |
| 50 | val = readl(mux->reg); |
| 51 | val &= ~(mux->mask << mux->shift); |
| 52 | val |= index << mux->shift; |
| 53 | fixup_mux->fixup(&val); |
| 54 | writel(val, mux->reg); |
| 55 | |
| 56 | spin_unlock_irqrestore(mux->lock, flags); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static const struct clk_ops clk_fixup_mux_ops = { |
| 62 | .get_parent = clk_fixup_mux_get_parent, |
| 63 | .set_parent = clk_fixup_mux_set_parent, |
| 64 | }; |
| 65 | |
| 66 | struct clk *imx_clk_fixup_mux(const char *name, void __iomem *reg, |
A.s. Dong | 9e5ef7a | 2018-11-14 13:02:00 +0000 | [diff] [blame] | 67 | u8 shift, u8 width, const char * const *parents, |
Liu Ying | a49e6c4 | 2013-07-04 17:35:46 +0800 | [diff] [blame] | 68 | int num_parents, void (*fixup)(u32 *val)) |
| 69 | { |
| 70 | struct clk_fixup_mux *fixup_mux; |
| 71 | struct clk *clk; |
| 72 | struct clk_init_data init; |
| 73 | |
| 74 | if (!fixup) |
| 75 | return ERR_PTR(-EINVAL); |
| 76 | |
| 77 | fixup_mux = kzalloc(sizeof(*fixup_mux), GFP_KERNEL); |
| 78 | if (!fixup_mux) |
| 79 | return ERR_PTR(-ENOMEM); |
| 80 | |
| 81 | init.name = name; |
| 82 | init.ops = &clk_fixup_mux_ops; |
| 83 | init.parent_names = parents; |
| 84 | init.num_parents = num_parents; |
Shawn Guo | bdb1b5f | 2013-09-04 20:49:04 +0800 | [diff] [blame] | 85 | init.flags = 0; |
Liu Ying | a49e6c4 | 2013-07-04 17:35:46 +0800 | [diff] [blame] | 86 | |
| 87 | fixup_mux->mux.reg = reg; |
| 88 | fixup_mux->mux.shift = shift; |
| 89 | fixup_mux->mux.mask = BIT(width) - 1; |
| 90 | fixup_mux->mux.lock = &imx_ccm_lock; |
| 91 | fixup_mux->mux.hw.init = &init; |
| 92 | fixup_mux->ops = &clk_mux_ops; |
| 93 | fixup_mux->fixup = fixup; |
| 94 | |
| 95 | clk = clk_register(NULL, &fixup_mux->mux.hw); |
| 96 | if (IS_ERR(clk)) |
| 97 | kfree(fixup_mux); |
| 98 | |
| 99 | return clk; |
| 100 | } |