18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Hisilicon hi6220 SoC divider clock driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2015 Hisilicon Limited.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Bintian Wang <bintian.wang@huawei.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/io.h>
148c2ecf20Sopenharmony_ci#include <linux/err.h>
158c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "clk.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define div_mask(width)	((1 << (width)) - 1)
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/**
228c2ecf20Sopenharmony_ci * struct hi6220_clk_divider - divider clock for hi6220
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * @hw:		handle between common and hardware-specific interfaces
258c2ecf20Sopenharmony_ci * @reg:	register containing divider
268c2ecf20Sopenharmony_ci * @shift:	shift to the divider bit field
278c2ecf20Sopenharmony_ci * @width:	width of the divider bit field
288c2ecf20Sopenharmony_ci * @mask:	mask for setting divider rate
298c2ecf20Sopenharmony_ci * @table:	the div table that the divider supports
308c2ecf20Sopenharmony_ci * @lock:	register lock
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_cistruct hi6220_clk_divider {
338c2ecf20Sopenharmony_ci	struct clk_hw	hw;
348c2ecf20Sopenharmony_ci	void __iomem	*reg;
358c2ecf20Sopenharmony_ci	u8		shift;
368c2ecf20Sopenharmony_ci	u8		width;
378c2ecf20Sopenharmony_ci	u32		mask;
388c2ecf20Sopenharmony_ci	const struct clk_div_table *table;
398c2ecf20Sopenharmony_ci	spinlock_t	*lock;
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define to_hi6220_clk_divider(_hw)	\
438c2ecf20Sopenharmony_ci	container_of(_hw, struct hi6220_clk_divider, hw)
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic unsigned long hi6220_clkdiv_recalc_rate(struct clk_hw *hw,
468c2ecf20Sopenharmony_ci					unsigned long parent_rate)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	unsigned int val;
498c2ecf20Sopenharmony_ci	struct hi6220_clk_divider *dclk = to_hi6220_clk_divider(hw);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	val = readl_relaxed(dclk->reg) >> dclk->shift;
528c2ecf20Sopenharmony_ci	val &= div_mask(dclk->width);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	return divider_recalc_rate(hw, parent_rate, val, dclk->table,
558c2ecf20Sopenharmony_ci				   CLK_DIVIDER_ROUND_CLOSEST, dclk->width);
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic long hi6220_clkdiv_round_rate(struct clk_hw *hw, unsigned long rate,
598c2ecf20Sopenharmony_ci					unsigned long *prate)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	struct hi6220_clk_divider *dclk = to_hi6220_clk_divider(hw);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	return divider_round_rate(hw, rate, prate, dclk->table,
648c2ecf20Sopenharmony_ci				  dclk->width, CLK_DIVIDER_ROUND_CLOSEST);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic int hi6220_clkdiv_set_rate(struct clk_hw *hw, unsigned long rate,
688c2ecf20Sopenharmony_ci					unsigned long parent_rate)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	int value;
718c2ecf20Sopenharmony_ci	unsigned long flags = 0;
728c2ecf20Sopenharmony_ci	u32 data;
738c2ecf20Sopenharmony_ci	struct hi6220_clk_divider *dclk = to_hi6220_clk_divider(hw);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	value = divider_get_val(rate, parent_rate, dclk->table,
768c2ecf20Sopenharmony_ci				dclk->width, CLK_DIVIDER_ROUND_CLOSEST);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (dclk->lock)
798c2ecf20Sopenharmony_ci		spin_lock_irqsave(dclk->lock, flags);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	data = readl_relaxed(dclk->reg);
828c2ecf20Sopenharmony_ci	data &= ~(div_mask(dclk->width) << dclk->shift);
838c2ecf20Sopenharmony_ci	data |= value << dclk->shift;
848c2ecf20Sopenharmony_ci	data |= dclk->mask;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	writel_relaxed(data, dclk->reg);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	if (dclk->lock)
898c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(dclk->lock, flags);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	return 0;
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic const struct clk_ops hi6220_clkdiv_ops = {
958c2ecf20Sopenharmony_ci	.recalc_rate = hi6220_clkdiv_recalc_rate,
968c2ecf20Sopenharmony_ci	.round_rate = hi6220_clkdiv_round_rate,
978c2ecf20Sopenharmony_ci	.set_rate = hi6220_clkdiv_set_rate,
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistruct clk *hi6220_register_clkdiv(struct device *dev, const char *name,
1018c2ecf20Sopenharmony_ci	const char *parent_name, unsigned long flags, void __iomem *reg,
1028c2ecf20Sopenharmony_ci	u8 shift, u8 width, u32 mask_bit, spinlock_t *lock)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	struct hi6220_clk_divider *div;
1058c2ecf20Sopenharmony_ci	struct clk *clk;
1068c2ecf20Sopenharmony_ci	struct clk_init_data init;
1078c2ecf20Sopenharmony_ci	struct clk_div_table *table;
1088c2ecf20Sopenharmony_ci	u32 max_div, min_div;
1098c2ecf20Sopenharmony_ci	int i;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	/* allocate the divider */
1128c2ecf20Sopenharmony_ci	div = kzalloc(sizeof(*div), GFP_KERNEL);
1138c2ecf20Sopenharmony_ci	if (!div)
1148c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	/* Init the divider table */
1178c2ecf20Sopenharmony_ci	max_div = div_mask(width) + 1;
1188c2ecf20Sopenharmony_ci	min_div = 1;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	table = kcalloc(max_div + 1, sizeof(*table), GFP_KERNEL);
1218c2ecf20Sopenharmony_ci	if (!table) {
1228c2ecf20Sopenharmony_ci		kfree(div);
1238c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	for (i = 0; i < max_div; i++) {
1278c2ecf20Sopenharmony_ci		table[i].div = min_div + i;
1288c2ecf20Sopenharmony_ci		table[i].val = table[i].div - 1;
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	init.name = name;
1328c2ecf20Sopenharmony_ci	init.ops = &hi6220_clkdiv_ops;
1338c2ecf20Sopenharmony_ci	init.flags = flags;
1348c2ecf20Sopenharmony_ci	init.parent_names = parent_name ? &parent_name : NULL;
1358c2ecf20Sopenharmony_ci	init.num_parents = parent_name ? 1 : 0;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	/* struct hi6220_clk_divider assignments */
1388c2ecf20Sopenharmony_ci	div->reg = reg;
1398c2ecf20Sopenharmony_ci	div->shift = shift;
1408c2ecf20Sopenharmony_ci	div->width = width;
1418c2ecf20Sopenharmony_ci	div->mask = mask_bit ? BIT(mask_bit) : 0;
1428c2ecf20Sopenharmony_ci	div->lock = lock;
1438c2ecf20Sopenharmony_ci	div->hw.init = &init;
1448c2ecf20Sopenharmony_ci	div->table = table;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	/* register the clock */
1478c2ecf20Sopenharmony_ci	clk = clk_register(dev, &div->hw);
1488c2ecf20Sopenharmony_ci	if (IS_ERR(clk)) {
1498c2ecf20Sopenharmony_ci		kfree(table);
1508c2ecf20Sopenharmony_ci		kfree(div);
1518c2ecf20Sopenharmony_ci	}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	return clk;
1548c2ecf20Sopenharmony_ci}
155