1f9419783STaniya Das // SPDX-License-Identifier: GPL-2.0
2085d7a45SStephen Boyd /*
3085d7a45SStephen Boyd * Copyright (c) 2014, The Linux Foundation. All rights reserved.
4085d7a45SStephen Boyd */
5085d7a45SStephen Boyd
6085d7a45SStephen Boyd #include <linux/device.h>
7085d7a45SStephen Boyd #include <linux/clk-provider.h>
8085d7a45SStephen Boyd #include <linux/regmap.h>
9085d7a45SStephen Boyd #include <linux/export.h>
10085d7a45SStephen Boyd
11085d7a45SStephen Boyd #include "clk-regmap.h"
12085d7a45SStephen Boyd
13085d7a45SStephen Boyd /**
14085d7a45SStephen Boyd * clk_is_enabled_regmap - standard is_enabled() for regmap users
15085d7a45SStephen Boyd *
16085d7a45SStephen Boyd * @hw: clk to operate on
17085d7a45SStephen Boyd *
18085d7a45SStephen Boyd * Clocks that use regmap for their register I/O can set the
19085d7a45SStephen Boyd * enable_reg and enable_mask fields in their struct clk_regmap and then use
20085d7a45SStephen Boyd * this as their is_enabled operation, saving some code.
21085d7a45SStephen Boyd */
clk_is_enabled_regmap(struct clk_hw * hw)22085d7a45SStephen Boyd int clk_is_enabled_regmap(struct clk_hw *hw)
23085d7a45SStephen Boyd {
24085d7a45SStephen Boyd struct clk_regmap *rclk = to_clk_regmap(hw);
25085d7a45SStephen Boyd unsigned int val;
26085d7a45SStephen Boyd int ret;
27085d7a45SStephen Boyd
28085d7a45SStephen Boyd ret = regmap_read(rclk->regmap, rclk->enable_reg, &val);
29085d7a45SStephen Boyd if (ret != 0)
30085d7a45SStephen Boyd return ret;
31085d7a45SStephen Boyd
32085d7a45SStephen Boyd if (rclk->enable_is_inverted)
33085d7a45SStephen Boyd return (val & rclk->enable_mask) == 0;
34085d7a45SStephen Boyd else
35085d7a45SStephen Boyd return (val & rclk->enable_mask) != 0;
36085d7a45SStephen Boyd }
37085d7a45SStephen Boyd EXPORT_SYMBOL_GPL(clk_is_enabled_regmap);
38085d7a45SStephen Boyd
39085d7a45SStephen Boyd /**
40085d7a45SStephen Boyd * clk_enable_regmap - standard enable() for regmap users
41085d7a45SStephen Boyd *
42085d7a45SStephen Boyd * @hw: clk to operate on
43085d7a45SStephen Boyd *
44085d7a45SStephen Boyd * Clocks that use regmap for their register I/O can set the
45085d7a45SStephen Boyd * enable_reg and enable_mask fields in their struct clk_regmap and then use
46085d7a45SStephen Boyd * this as their enable() operation, saving some code.
47085d7a45SStephen Boyd */
clk_enable_regmap(struct clk_hw * hw)48085d7a45SStephen Boyd int clk_enable_regmap(struct clk_hw *hw)
49085d7a45SStephen Boyd {
50085d7a45SStephen Boyd struct clk_regmap *rclk = to_clk_regmap(hw);
51085d7a45SStephen Boyd unsigned int val;
52085d7a45SStephen Boyd
53085d7a45SStephen Boyd if (rclk->enable_is_inverted)
54085d7a45SStephen Boyd val = 0;
55085d7a45SStephen Boyd else
56085d7a45SStephen Boyd val = rclk->enable_mask;
57085d7a45SStephen Boyd
58085d7a45SStephen Boyd return regmap_update_bits(rclk->regmap, rclk->enable_reg,
59085d7a45SStephen Boyd rclk->enable_mask, val);
60085d7a45SStephen Boyd }
61085d7a45SStephen Boyd EXPORT_SYMBOL_GPL(clk_enable_regmap);
62085d7a45SStephen Boyd
63085d7a45SStephen Boyd /**
64085d7a45SStephen Boyd * clk_disable_regmap - standard disable() for regmap users
65085d7a45SStephen Boyd *
66085d7a45SStephen Boyd * @hw: clk to operate on
67085d7a45SStephen Boyd *
68085d7a45SStephen Boyd * Clocks that use regmap for their register I/O can set the
69085d7a45SStephen Boyd * enable_reg and enable_mask fields in their struct clk_regmap and then use
70085d7a45SStephen Boyd * this as their disable() operation, saving some code.
71085d7a45SStephen Boyd */
clk_disable_regmap(struct clk_hw * hw)72085d7a45SStephen Boyd void clk_disable_regmap(struct clk_hw *hw)
73085d7a45SStephen Boyd {
74085d7a45SStephen Boyd struct clk_regmap *rclk = to_clk_regmap(hw);
75085d7a45SStephen Boyd unsigned int val;
76085d7a45SStephen Boyd
77085d7a45SStephen Boyd if (rclk->enable_is_inverted)
78085d7a45SStephen Boyd val = rclk->enable_mask;
79085d7a45SStephen Boyd else
80085d7a45SStephen Boyd val = 0;
81085d7a45SStephen Boyd
82085d7a45SStephen Boyd regmap_update_bits(rclk->regmap, rclk->enable_reg, rclk->enable_mask,
83085d7a45SStephen Boyd val);
84085d7a45SStephen Boyd }
85085d7a45SStephen Boyd EXPORT_SYMBOL_GPL(clk_disable_regmap);
86085d7a45SStephen Boyd
87085d7a45SStephen Boyd /**
88085d7a45SStephen Boyd * devm_clk_register_regmap - register a clk_regmap clock
89085d7a45SStephen Boyd *
90*4414c16eSLee Jones * @dev: reference to the caller's device
91085d7a45SStephen Boyd * @rclk: clk to operate on
92085d7a45SStephen Boyd *
93085d7a45SStephen Boyd * Clocks that use regmap for their register I/O should register their
94085d7a45SStephen Boyd * clk_regmap struct via this function so that the regmap is initialized
95085d7a45SStephen Boyd * and so that the clock is registered with the common clock framework.
96085d7a45SStephen Boyd */
devm_clk_register_regmap(struct device * dev,struct clk_regmap * rclk)97120c1552SStephen Boyd int devm_clk_register_regmap(struct device *dev, struct clk_regmap *rclk)
98085d7a45SStephen Boyd {
99085d7a45SStephen Boyd if (dev && dev_get_regmap(dev, NULL))
100085d7a45SStephen Boyd rclk->regmap = dev_get_regmap(dev, NULL);
101085d7a45SStephen Boyd else if (dev && dev->parent)
102085d7a45SStephen Boyd rclk->regmap = dev_get_regmap(dev->parent, NULL);
103085d7a45SStephen Boyd
104120c1552SStephen Boyd return devm_clk_hw_register(dev, &rclk->hw);
105085d7a45SStephen Boyd }
106085d7a45SStephen Boyd EXPORT_SYMBOL_GPL(devm_clk_register_regmap);
107