drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / clk / uniphier / clk-uniphier-mux.c
blob1998e9d4cfc0224aebc09e2dddc484c49e1e24c3
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2016 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
5 */
7 #include <linux/clk-provider.h>
8 #include <linux/device.h>
9 #include <linux/regmap.h>
11 #include "clk-uniphier.h"
13 struct uniphier_clk_mux {
14 struct clk_hw hw;
15 struct regmap *regmap;
16 unsigned int reg;
17 const unsigned int *masks;
18 const unsigned int *vals;
21 #define to_uniphier_clk_mux(_hw) container_of(_hw, struct uniphier_clk_mux, hw)
23 static int uniphier_clk_mux_set_parent(struct clk_hw *hw, u8 index)
25 struct uniphier_clk_mux *mux = to_uniphier_clk_mux(hw);
27 return regmap_write_bits(mux->regmap, mux->reg, mux->masks[index],
28 mux->vals[index]);
31 static u8 uniphier_clk_mux_get_parent(struct clk_hw *hw)
33 struct uniphier_clk_mux *mux = to_uniphier_clk_mux(hw);
34 unsigned int num_parents = clk_hw_get_num_parents(hw);
35 int ret;
36 unsigned int val;
37 unsigned int i;
39 ret = regmap_read(mux->regmap, mux->reg, &val);
40 if (ret)
41 return ret;
43 for (i = 0; i < num_parents; i++)
44 if ((mux->masks[i] & val) == mux->vals[i])
45 return i;
47 return -EINVAL;
50 static const struct clk_ops uniphier_clk_mux_ops = {
51 .determine_rate = __clk_mux_determine_rate,
52 .set_parent = uniphier_clk_mux_set_parent,
53 .get_parent = uniphier_clk_mux_get_parent,
56 struct clk_hw *uniphier_clk_register_mux(struct device *dev,
57 struct regmap *regmap,
58 const char *name,
59 const struct uniphier_clk_mux_data *data)
61 struct uniphier_clk_mux *mux;
62 struct clk_init_data init;
63 int ret;
65 mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
66 if (!mux)
67 return ERR_PTR(-ENOMEM);
69 init.name = name;
70 init.ops = &uniphier_clk_mux_ops;
71 init.flags = CLK_SET_RATE_PARENT;
72 init.parent_names = data->parent_names;
73 init.num_parents = data->num_parents;
75 mux->regmap = regmap;
76 mux->reg = data->reg;
77 mux->masks = data->masks;
78 mux->vals = data->vals;
79 mux->hw.init = &init;
81 ret = devm_clk_hw_register(dev, &mux->hw);
82 if (ret)
83 return ERR_PTR(ret);
85 return &mux->hw;