xref: /linux/drivers/clk/hisilicon/clk-hi3660-stub.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
24f16f7ffSKaihua Zhong /*
34f16f7ffSKaihua Zhong  * Hisilicon clock driver
44f16f7ffSKaihua Zhong  *
54f16f7ffSKaihua Zhong  * Copyright (c) 2013-2017 Hisilicon Limited.
64f16f7ffSKaihua Zhong  * Copyright (c) 2017 Linaro Limited.
74f16f7ffSKaihua Zhong  *
84f16f7ffSKaihua Zhong  * Author: Kai Zhao <zhaokai1@hisilicon.com>
94f16f7ffSKaihua Zhong  *	    Tao Wang <kevin.wangtao@hisilicon.com>
104f16f7ffSKaihua Zhong  *	    Leo Yan <leo.yan@linaro.org>
114f16f7ffSKaihua Zhong  */
124f16f7ffSKaihua Zhong 
134f16f7ffSKaihua Zhong #include <linux/clk-provider.h>
144f16f7ffSKaihua Zhong #include <linux/device.h>
154f16f7ffSKaihua Zhong #include <linux/err.h>
164f16f7ffSKaihua Zhong #include <linux/init.h>
1762e59c4eSStephen Boyd #include <linux/io.h>
184f16f7ffSKaihua Zhong #include <linux/mailbox_client.h>
194f16f7ffSKaihua Zhong #include <linux/module.h>
204f16f7ffSKaihua Zhong #include <linux/of.h>
214f16f7ffSKaihua Zhong #include <linux/platform_device.h>
224f16f7ffSKaihua Zhong #include <dt-bindings/clock/hi3660-clock.h>
234f16f7ffSKaihua Zhong 
244f16f7ffSKaihua Zhong #define HI3660_STUB_CLOCK_DATA		(0x70)
254f16f7ffSKaihua Zhong #define MHZ				(1000 * 1000)
264f16f7ffSKaihua Zhong 
274f16f7ffSKaihua Zhong #define DEFINE_CLK_STUB(_id, _cmd, _name)			\
284f16f7ffSKaihua Zhong 	{							\
294f16f7ffSKaihua Zhong 		.id = (_id),					\
304f16f7ffSKaihua Zhong 		.cmd = (_cmd),					\
314f16f7ffSKaihua Zhong 		.hw.init = &(struct clk_init_data) {		\
324f16f7ffSKaihua Zhong 			.name = #_name,				\
334f16f7ffSKaihua Zhong 			.ops = &hi3660_stub_clk_ops,		\
344f16f7ffSKaihua Zhong 			.num_parents = 0,			\
354f16f7ffSKaihua Zhong 			.flags = CLK_GET_RATE_NOCACHE,		\
364f16f7ffSKaihua Zhong 		},						\
374f16f7ffSKaihua Zhong 	},
384f16f7ffSKaihua Zhong 
394f16f7ffSKaihua Zhong #define to_stub_clk(_hw) container_of(_hw, struct hi3660_stub_clk, hw)
404f16f7ffSKaihua Zhong 
414f16f7ffSKaihua Zhong struct hi3660_stub_clk_chan {
424f16f7ffSKaihua Zhong 	struct mbox_client cl;
434f16f7ffSKaihua Zhong 	struct mbox_chan *mbox;
444f16f7ffSKaihua Zhong };
454f16f7ffSKaihua Zhong 
464f16f7ffSKaihua Zhong struct hi3660_stub_clk {
474f16f7ffSKaihua Zhong 	unsigned int id;
484f16f7ffSKaihua Zhong 	struct clk_hw hw;
494f16f7ffSKaihua Zhong 	unsigned int cmd;
504f16f7ffSKaihua Zhong 	unsigned int msg[8];
514f16f7ffSKaihua Zhong 	unsigned int rate;
524f16f7ffSKaihua Zhong };
534f16f7ffSKaihua Zhong 
544f16f7ffSKaihua Zhong static void __iomem *freq_reg;
554f16f7ffSKaihua Zhong static struct hi3660_stub_clk_chan stub_clk_chan;
564f16f7ffSKaihua Zhong 
hi3660_stub_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)574f16f7ffSKaihua Zhong static unsigned long hi3660_stub_clk_recalc_rate(struct clk_hw *hw,
584f16f7ffSKaihua Zhong 						 unsigned long parent_rate)
594f16f7ffSKaihua Zhong {
604f16f7ffSKaihua Zhong 	struct hi3660_stub_clk *stub_clk = to_stub_clk(hw);
614f16f7ffSKaihua Zhong 
624f16f7ffSKaihua Zhong 	/*
634f16f7ffSKaihua Zhong 	 * LPM3 writes back the CPU frequency in shared SRAM so read
644f16f7ffSKaihua Zhong 	 * back the frequency.
654f16f7ffSKaihua Zhong 	 */
664f16f7ffSKaihua Zhong 	stub_clk->rate = readl(freq_reg + (stub_clk->id << 2)) * MHZ;
674f16f7ffSKaihua Zhong 	return stub_clk->rate;
684f16f7ffSKaihua Zhong }
694f16f7ffSKaihua Zhong 
hi3660_stub_clk_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)704f16f7ffSKaihua Zhong static long hi3660_stub_clk_round_rate(struct clk_hw *hw, unsigned long rate,
714f16f7ffSKaihua Zhong 				       unsigned long *prate)
724f16f7ffSKaihua Zhong {
734f16f7ffSKaihua Zhong 	/*
744f16f7ffSKaihua Zhong 	 * LPM3 handles rate rounding so just return whatever
754f16f7ffSKaihua Zhong 	 * rate is requested.
764f16f7ffSKaihua Zhong 	 */
774f16f7ffSKaihua Zhong 	return rate;
784f16f7ffSKaihua Zhong }
794f16f7ffSKaihua Zhong 
hi3660_stub_clk_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)804f16f7ffSKaihua Zhong static int hi3660_stub_clk_set_rate(struct clk_hw *hw, unsigned long rate,
814f16f7ffSKaihua Zhong 				    unsigned long parent_rate)
824f16f7ffSKaihua Zhong {
834f16f7ffSKaihua Zhong 	struct hi3660_stub_clk *stub_clk = to_stub_clk(hw);
844f16f7ffSKaihua Zhong 
854f16f7ffSKaihua Zhong 	stub_clk->msg[0] = stub_clk->cmd;
864f16f7ffSKaihua Zhong 	stub_clk->msg[1] = rate / MHZ;
874f16f7ffSKaihua Zhong 
884f16f7ffSKaihua Zhong 	dev_dbg(stub_clk_chan.cl.dev, "set rate msg[0]=0x%x msg[1]=0x%x\n",
894f16f7ffSKaihua Zhong 		stub_clk->msg[0], stub_clk->msg[1]);
904f16f7ffSKaihua Zhong 
914f16f7ffSKaihua Zhong 	mbox_send_message(stub_clk_chan.mbox, stub_clk->msg);
924f16f7ffSKaihua Zhong 	mbox_client_txdone(stub_clk_chan.mbox, 0);
934f16f7ffSKaihua Zhong 
944f16f7ffSKaihua Zhong 	stub_clk->rate = rate;
954f16f7ffSKaihua Zhong 	return 0;
964f16f7ffSKaihua Zhong }
974f16f7ffSKaihua Zhong 
984f16f7ffSKaihua Zhong static const struct clk_ops hi3660_stub_clk_ops = {
994f16f7ffSKaihua Zhong 	.recalc_rate    = hi3660_stub_clk_recalc_rate,
1004f16f7ffSKaihua Zhong 	.round_rate     = hi3660_stub_clk_round_rate,
1014f16f7ffSKaihua Zhong 	.set_rate       = hi3660_stub_clk_set_rate,
1024f16f7ffSKaihua Zhong };
1034f16f7ffSKaihua Zhong 
1044f16f7ffSKaihua Zhong static struct hi3660_stub_clk hi3660_stub_clks[HI3660_CLK_STUB_NUM] = {
1054f16f7ffSKaihua Zhong 	DEFINE_CLK_STUB(HI3660_CLK_STUB_CLUSTER0, 0x0001030A, "cpu-cluster.0")
1064f16f7ffSKaihua Zhong 	DEFINE_CLK_STUB(HI3660_CLK_STUB_CLUSTER1, 0x0002030A, "cpu-cluster.1")
1074f16f7ffSKaihua Zhong 	DEFINE_CLK_STUB(HI3660_CLK_STUB_GPU, 0x0003030A, "clk-g3d")
1084f16f7ffSKaihua Zhong 	DEFINE_CLK_STUB(HI3660_CLK_STUB_DDR, 0x00040309, "clk-ddrc")
1094f16f7ffSKaihua Zhong };
1104f16f7ffSKaihua Zhong 
hi3660_stub_clk_hw_get(struct of_phandle_args * clkspec,void * data)1114f16f7ffSKaihua Zhong static struct clk_hw *hi3660_stub_clk_hw_get(struct of_phandle_args *clkspec,
1124f16f7ffSKaihua Zhong 					     void *data)
1134f16f7ffSKaihua Zhong {
1144f16f7ffSKaihua Zhong 	unsigned int idx = clkspec->args[0];
1154f16f7ffSKaihua Zhong 
1164f16f7ffSKaihua Zhong 	if (idx >= HI3660_CLK_STUB_NUM) {
1174f16f7ffSKaihua Zhong 		pr_err("%s: invalid index %u\n", __func__, idx);
1184f16f7ffSKaihua Zhong 		return ERR_PTR(-EINVAL);
1194f16f7ffSKaihua Zhong 	}
1204f16f7ffSKaihua Zhong 
1214f16f7ffSKaihua Zhong 	return &hi3660_stub_clks[idx].hw;
1224f16f7ffSKaihua Zhong }
1234f16f7ffSKaihua Zhong 
hi3660_stub_clk_probe(struct platform_device * pdev)1244f16f7ffSKaihua Zhong static int hi3660_stub_clk_probe(struct platform_device *pdev)
1254f16f7ffSKaihua Zhong {
1264f16f7ffSKaihua Zhong 	struct device *dev = &pdev->dev;
1274f16f7ffSKaihua Zhong 	struct resource *res;
1284f16f7ffSKaihua Zhong 	unsigned int i;
1294f16f7ffSKaihua Zhong 	int ret;
1304f16f7ffSKaihua Zhong 
1314f16f7ffSKaihua Zhong 	/* Use mailbox client without blocking */
1324f16f7ffSKaihua Zhong 	stub_clk_chan.cl.dev = dev;
1334f16f7ffSKaihua Zhong 	stub_clk_chan.cl.tx_done = NULL;
1344f16f7ffSKaihua Zhong 	stub_clk_chan.cl.tx_block = false;
1354f16f7ffSKaihua Zhong 	stub_clk_chan.cl.knows_txdone = false;
1364f16f7ffSKaihua Zhong 
1374f16f7ffSKaihua Zhong 	/* Allocate mailbox channel */
1384f16f7ffSKaihua Zhong 	stub_clk_chan.mbox = mbox_request_channel(&stub_clk_chan.cl, 0);
1394f16f7ffSKaihua Zhong 	if (IS_ERR(stub_clk_chan.mbox))
1404f16f7ffSKaihua Zhong 		return PTR_ERR(stub_clk_chan.mbox);
1414f16f7ffSKaihua Zhong 
1424f16f7ffSKaihua Zhong 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1439903e41aSWei Yongjun 	if (!res)
1449903e41aSWei Yongjun 		return -EINVAL;
1454f16f7ffSKaihua Zhong 	freq_reg = devm_ioremap(dev, res->start, resource_size(res));
1464f16f7ffSKaihua Zhong 	if (!freq_reg)
1474f16f7ffSKaihua Zhong 		return -ENOMEM;
1484f16f7ffSKaihua Zhong 
1494f16f7ffSKaihua Zhong 	freq_reg += HI3660_STUB_CLOCK_DATA;
1504f16f7ffSKaihua Zhong 
1514f16f7ffSKaihua Zhong 	for (i = 0; i < HI3660_CLK_STUB_NUM; i++) {
1524f16f7ffSKaihua Zhong 		ret = devm_clk_hw_register(&pdev->dev, &hi3660_stub_clks[i].hw);
1534f16f7ffSKaihua Zhong 		if (ret)
1544f16f7ffSKaihua Zhong 			return ret;
1554f16f7ffSKaihua Zhong 	}
1564f16f7ffSKaihua Zhong 
1574f16f7ffSKaihua Zhong 	return devm_of_clk_add_hw_provider(&pdev->dev, hi3660_stub_clk_hw_get,
1584f16f7ffSKaihua Zhong 					   hi3660_stub_clks);
1594f16f7ffSKaihua Zhong }
1604f16f7ffSKaihua Zhong 
1614f16f7ffSKaihua Zhong static const struct of_device_id hi3660_stub_clk_of_match[] = {
1624f16f7ffSKaihua Zhong 	{ .compatible = "hisilicon,hi3660-stub-clk", },
1634f16f7ffSKaihua Zhong 	{}
1644f16f7ffSKaihua Zhong };
1654f16f7ffSKaihua Zhong 
1664f16f7ffSKaihua Zhong static struct platform_driver hi3660_stub_clk_driver = {
1674f16f7ffSKaihua Zhong 	.probe	= hi3660_stub_clk_probe,
1684f16f7ffSKaihua Zhong 	.driver = {
1694f16f7ffSKaihua Zhong 		.name = "hi3660-stub-clk",
1704f16f7ffSKaihua Zhong 		.of_match_table = hi3660_stub_clk_of_match,
1714f16f7ffSKaihua Zhong 	},
1724f16f7ffSKaihua Zhong };
1734f16f7ffSKaihua Zhong 
hi3660_stub_clk_init(void)1744f16f7ffSKaihua Zhong static int __init hi3660_stub_clk_init(void)
1754f16f7ffSKaihua Zhong {
1764f16f7ffSKaihua Zhong 	return platform_driver_register(&hi3660_stub_clk_driver);
1774f16f7ffSKaihua Zhong }
1784f16f7ffSKaihua Zhong subsys_initcall(hi3660_stub_clk_init);
179