18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2012 Freescale Semiconductor, Inc.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
78c2ecf20Sopenharmony_ci#include <linux/err.h>
88c2ecf20Sopenharmony_ci#include <linux/slab.h>
98c2ecf20Sopenharmony_ci#include "clk.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/**
128c2ecf20Sopenharmony_ci * struct clk_div - mxs integer divider clock
138c2ecf20Sopenharmony_ci * @divider: the parent class
148c2ecf20Sopenharmony_ci * @ops: pointer to clk_ops of parent class
158c2ecf20Sopenharmony_ci * @reg: register address
168c2ecf20Sopenharmony_ci * @busy: busy bit shift
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * The mxs divider clock is a subclass of basic clk_divider with an
198c2ecf20Sopenharmony_ci * addtional busy bit.
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_cistruct clk_div {
228c2ecf20Sopenharmony_ci	struct clk_divider divider;
238c2ecf20Sopenharmony_ci	const struct clk_ops *ops;
248c2ecf20Sopenharmony_ci	void __iomem *reg;
258c2ecf20Sopenharmony_ci	u8 busy;
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic inline struct clk_div *to_clk_div(struct clk_hw *hw)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	struct clk_divider *divider = to_clk_divider(hw);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	return container_of(divider, struct clk_div, divider);
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic unsigned long clk_div_recalc_rate(struct clk_hw *hw,
368c2ecf20Sopenharmony_ci					 unsigned long parent_rate)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct clk_div *div = to_clk_div(hw);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	return div->ops->recalc_rate(&div->divider.hw, parent_rate);
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic long clk_div_round_rate(struct clk_hw *hw, unsigned long rate,
448c2ecf20Sopenharmony_ci			       unsigned long *prate)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	struct clk_div *div = to_clk_div(hw);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	return div->ops->round_rate(&div->divider.hw, rate, prate);
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
528c2ecf20Sopenharmony_ci			    unsigned long parent_rate)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	struct clk_div *div = to_clk_div(hw);
558c2ecf20Sopenharmony_ci	int ret;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	ret = div->ops->set_rate(&div->divider.hw, rate, parent_rate);
588c2ecf20Sopenharmony_ci	if (!ret)
598c2ecf20Sopenharmony_ci		ret = mxs_clk_wait(div->reg, div->busy);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	return ret;
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic const struct clk_ops clk_div_ops = {
658c2ecf20Sopenharmony_ci	.recalc_rate = clk_div_recalc_rate,
668c2ecf20Sopenharmony_ci	.round_rate = clk_div_round_rate,
678c2ecf20Sopenharmony_ci	.set_rate = clk_div_set_rate,
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistruct clk *mxs_clk_div(const char *name, const char *parent_name,
718c2ecf20Sopenharmony_ci			void __iomem *reg, u8 shift, u8 width, u8 busy)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	struct clk_div *div;
748c2ecf20Sopenharmony_ci	struct clk *clk;
758c2ecf20Sopenharmony_ci	struct clk_init_data init;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	div = kzalloc(sizeof(*div), GFP_KERNEL);
788c2ecf20Sopenharmony_ci	if (!div)
798c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	init.name = name;
828c2ecf20Sopenharmony_ci	init.ops = &clk_div_ops;
838c2ecf20Sopenharmony_ci	init.flags = CLK_SET_RATE_PARENT;
848c2ecf20Sopenharmony_ci	init.parent_names = (parent_name ? &parent_name: NULL);
858c2ecf20Sopenharmony_ci	init.num_parents = (parent_name ? 1 : 0);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	div->reg = reg;
888c2ecf20Sopenharmony_ci	div->busy = busy;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	div->divider.reg = reg;
918c2ecf20Sopenharmony_ci	div->divider.shift = shift;
928c2ecf20Sopenharmony_ci	div->divider.width = width;
938c2ecf20Sopenharmony_ci	div->divider.flags = CLK_DIVIDER_ONE_BASED;
948c2ecf20Sopenharmony_ci	div->divider.lock = &mxs_lock;
958c2ecf20Sopenharmony_ci	div->divider.hw.init = &init;
968c2ecf20Sopenharmony_ci	div->ops = &clk_divider_ops;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	clk = clk_register(NULL, &div->divider.hw);
998c2ecf20Sopenharmony_ci	if (IS_ERR(clk))
1008c2ecf20Sopenharmony_ci		kfree(div);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	return clk;
1038c2ecf20Sopenharmony_ci}
104