18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2014, The Linux Foundation. All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/device.h>
78c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
88c2ecf20Sopenharmony_ci#include <linux/regmap.h>
98c2ecf20Sopenharmony_ci#include <linux/export.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "clk-regmap.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/**
148c2ecf20Sopenharmony_ci * clk_is_enabled_regmap - standard is_enabled() for regmap users
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * @hw: clk to operate on
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * Clocks that use regmap for their register I/O can set the
198c2ecf20Sopenharmony_ci * enable_reg and enable_mask fields in their struct clk_regmap and then use
208c2ecf20Sopenharmony_ci * this as their is_enabled operation, saving some code.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ciint clk_is_enabled_regmap(struct clk_hw *hw)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	struct clk_regmap *rclk = to_clk_regmap(hw);
258c2ecf20Sopenharmony_ci	unsigned int val;
268c2ecf20Sopenharmony_ci	int ret;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	ret = regmap_read(rclk->regmap, rclk->enable_reg, &val);
298c2ecf20Sopenharmony_ci	if (ret != 0)
308c2ecf20Sopenharmony_ci		return ret;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	if (rclk->enable_is_inverted)
338c2ecf20Sopenharmony_ci		return (val & rclk->enable_mask) == 0;
348c2ecf20Sopenharmony_ci	else
358c2ecf20Sopenharmony_ci		return (val & rclk->enable_mask) != 0;
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_is_enabled_regmap);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/**
408c2ecf20Sopenharmony_ci * clk_enable_regmap - standard enable() for regmap users
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * @hw: clk to operate on
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * Clocks that use regmap for their register I/O can set the
458c2ecf20Sopenharmony_ci * enable_reg and enable_mask fields in their struct clk_regmap and then use
468c2ecf20Sopenharmony_ci * this as their enable() operation, saving some code.
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_ciint clk_enable_regmap(struct clk_hw *hw)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct clk_regmap *rclk = to_clk_regmap(hw);
518c2ecf20Sopenharmony_ci	unsigned int val;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	if (rclk->enable_is_inverted)
548c2ecf20Sopenharmony_ci		val = 0;
558c2ecf20Sopenharmony_ci	else
568c2ecf20Sopenharmony_ci		val = rclk->enable_mask;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	return regmap_update_bits(rclk->regmap, rclk->enable_reg,
598c2ecf20Sopenharmony_ci				  rclk->enable_mask, val);
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_enable_regmap);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/**
648c2ecf20Sopenharmony_ci * clk_disable_regmap - standard disable() for regmap users
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci * @hw: clk to operate on
678c2ecf20Sopenharmony_ci *
688c2ecf20Sopenharmony_ci * Clocks that use regmap for their register I/O can set the
698c2ecf20Sopenharmony_ci * enable_reg and enable_mask fields in their struct clk_regmap and then use
708c2ecf20Sopenharmony_ci * this as their disable() operation, saving some code.
718c2ecf20Sopenharmony_ci */
728c2ecf20Sopenharmony_civoid clk_disable_regmap(struct clk_hw *hw)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct clk_regmap *rclk = to_clk_regmap(hw);
758c2ecf20Sopenharmony_ci	unsigned int val;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	if (rclk->enable_is_inverted)
788c2ecf20Sopenharmony_ci		val = rclk->enable_mask;
798c2ecf20Sopenharmony_ci	else
808c2ecf20Sopenharmony_ci		val = 0;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	regmap_update_bits(rclk->regmap, rclk->enable_reg, rclk->enable_mask,
838c2ecf20Sopenharmony_ci			   val);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_disable_regmap);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/**
888c2ecf20Sopenharmony_ci * devm_clk_register_regmap - register a clk_regmap clock
898c2ecf20Sopenharmony_ci *
908c2ecf20Sopenharmony_ci * @rclk: clk to operate on
918c2ecf20Sopenharmony_ci *
928c2ecf20Sopenharmony_ci * Clocks that use regmap for their register I/O should register their
938c2ecf20Sopenharmony_ci * clk_regmap struct via this function so that the regmap is initialized
948c2ecf20Sopenharmony_ci * and so that the clock is registered with the common clock framework.
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_ciint devm_clk_register_regmap(struct device *dev, struct clk_regmap *rclk)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	if (dev && dev_get_regmap(dev, NULL))
998c2ecf20Sopenharmony_ci		rclk->regmap = dev_get_regmap(dev, NULL);
1008c2ecf20Sopenharmony_ci	else if (dev && dev->parent)
1018c2ecf20Sopenharmony_ci		rclk->regmap = dev_get_regmap(dev->parent, NULL);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	return devm_clk_hw_register(dev, &rclk->hw);
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_clk_register_regmap);
106