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