Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / clocksource / ingenic-ost.c
blob029efc2731b49f35b89f3b3c07011a2152f8d40b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * JZ47xx SoCs TCU Operating System Timer driver
5 * Copyright (C) 2016 Maarten ter Huurne <maarten@treewalker.org>
6 * Copyright (C) 2020 Paul Cercueil <paul@crapouillou.net>
7 */
9 #include <linux/clk.h>
10 #include <linux/clocksource.h>
11 #include <linux/mfd/ingenic-tcu.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm.h>
16 #include <linux/regmap.h>
17 #include <linux/sched_clock.h>
19 #define TCU_OST_TCSR_MASK 0xffc0
20 #define TCU_OST_TCSR_CNT_MD BIT(15)
22 #define TCU_OST_CHANNEL 15
25 * The TCU_REG_OST_CNT{L,R} from <linux/mfd/ingenic-tcu.h> are only for the
26 * regmap; these are for use with the __iomem pointer.
28 #define OST_REG_CNTL 0x4
29 #define OST_REG_CNTH 0x8
31 struct ingenic_ost_soc_info {
32 bool is64bit;
35 struct ingenic_ost {
36 void __iomem *regs;
37 struct clk *clk;
39 struct clocksource cs;
42 static struct ingenic_ost *ingenic_ost;
44 static u64 notrace ingenic_ost_read_cntl(void)
46 /* Read using __iomem pointer instead of regmap to avoid locking */
47 return readl(ingenic_ost->regs + OST_REG_CNTL);
50 static u64 notrace ingenic_ost_read_cnth(void)
52 /* Read using __iomem pointer instead of regmap to avoid locking */
53 return readl(ingenic_ost->regs + OST_REG_CNTH);
56 static u64 notrace ingenic_ost_clocksource_readl(struct clocksource *cs)
58 return ingenic_ost_read_cntl();
61 static u64 notrace ingenic_ost_clocksource_readh(struct clocksource *cs)
63 return ingenic_ost_read_cnth();
66 static int __init ingenic_ost_probe(struct platform_device *pdev)
68 const struct ingenic_ost_soc_info *soc_info;
69 struct device *dev = &pdev->dev;
70 struct ingenic_ost *ost;
71 struct clocksource *cs;
72 struct regmap *map;
73 unsigned long rate;
74 int err;
76 soc_info = device_get_match_data(dev);
77 if (!soc_info)
78 return -EINVAL;
80 ost = devm_kzalloc(dev, sizeof(*ost), GFP_KERNEL);
81 if (!ost)
82 return -ENOMEM;
84 ingenic_ost = ost;
86 ost->regs = devm_platform_ioremap_resource(pdev, 0);
87 if (IS_ERR(ost->regs))
88 return PTR_ERR(ost->regs);
90 map = device_node_to_regmap(dev->parent->of_node);
91 if (!map) {
92 dev_err(dev, "regmap not found");
93 return -EINVAL;
96 ost->clk = devm_clk_get(dev, "ost");
97 if (IS_ERR(ost->clk))
98 return PTR_ERR(ost->clk);
100 err = clk_prepare_enable(ost->clk);
101 if (err)
102 return err;
104 /* Clear counter high/low registers */
105 if (soc_info->is64bit)
106 regmap_write(map, TCU_REG_OST_CNTL, 0);
107 regmap_write(map, TCU_REG_OST_CNTH, 0);
109 /* Don't reset counter at compare value. */
110 regmap_update_bits(map, TCU_REG_OST_TCSR,
111 TCU_OST_TCSR_MASK, TCU_OST_TCSR_CNT_MD);
113 rate = clk_get_rate(ost->clk);
115 /* Enable OST TCU channel */
116 regmap_write(map, TCU_REG_TESR, BIT(TCU_OST_CHANNEL));
118 cs = &ost->cs;
119 cs->name = "ingenic-ost";
120 cs->rating = 320;
121 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
122 cs->mask = CLOCKSOURCE_MASK(32);
124 if (soc_info->is64bit)
125 cs->read = ingenic_ost_clocksource_readl;
126 else
127 cs->read = ingenic_ost_clocksource_readh;
129 err = clocksource_register_hz(cs, rate);
130 if (err) {
131 dev_err(dev, "clocksource registration failed");
132 clk_disable_unprepare(ost->clk);
133 return err;
136 if (soc_info->is64bit)
137 sched_clock_register(ingenic_ost_read_cntl, 32, rate);
138 else
139 sched_clock_register(ingenic_ost_read_cnth, 32, rate);
141 return 0;
144 static int __maybe_unused ingenic_ost_suspend(struct device *dev)
146 struct ingenic_ost *ost = dev_get_drvdata(dev);
148 clk_disable(ost->clk);
150 return 0;
153 static int __maybe_unused ingenic_ost_resume(struct device *dev)
155 struct ingenic_ost *ost = dev_get_drvdata(dev);
157 return clk_enable(ost->clk);
160 static const struct dev_pm_ops __maybe_unused ingenic_ost_pm_ops = {
161 /* _noirq: We want the OST clock to be gated last / ungated first */
162 .suspend_noirq = ingenic_ost_suspend,
163 .resume_noirq = ingenic_ost_resume,
166 static const struct ingenic_ost_soc_info jz4725b_ost_soc_info = {
167 .is64bit = false,
170 static const struct ingenic_ost_soc_info jz4770_ost_soc_info = {
171 .is64bit = true,
174 static const struct of_device_id ingenic_ost_of_match[] = {
175 { .compatible = "ingenic,jz4725b-ost", .data = &jz4725b_ost_soc_info, },
176 { .compatible = "ingenic,jz4770-ost", .data = &jz4770_ost_soc_info, },
180 static struct platform_driver ingenic_ost_driver = {
181 .driver = {
182 .name = "ingenic-ost",
183 #ifdef CONFIG_PM_SUSPEND
184 .pm = &ingenic_ost_pm_ops,
185 #endif
186 .of_match_table = ingenic_ost_of_match,
189 builtin_platform_driver_probe(ingenic_ost_driver, ingenic_ost_probe);