1d37010a3SWen He // SPDX-License-Identifier: GPL-2.0
2d37010a3SWen He /*
3d37010a3SWen He * Copyright 2019 NXP
4d37010a3SWen He *
5d37010a3SWen He * Clock driver for LS1028A Display output interfaces(LCD, DPHY).
6d37010a3SWen He */
7d37010a3SWen He
8d37010a3SWen He #include <linux/clk-provider.h>
9d37010a3SWen He #include <linux/device.h>
10d37010a3SWen He #include <linux/module.h>
11d37010a3SWen He #include <linux/err.h>
12d37010a3SWen He #include <linux/io.h>
13d37010a3SWen He #include <linux/iopoll.h>
14d37010a3SWen He #include <linux/of.h>
15d37010a3SWen He #include <linux/platform_device.h>
16d37010a3SWen He #include <linux/slab.h>
17d37010a3SWen He #include <linux/bitfield.h>
18d37010a3SWen He
19d37010a3SWen He /* PLLDIG register offsets and bit masks */
20d37010a3SWen He #define PLLDIG_REG_PLLSR 0x24
21d37010a3SWen He #define PLLDIG_LOCK_MASK BIT(2)
22d37010a3SWen He #define PLLDIG_REG_PLLDV 0x28
23d37010a3SWen He #define PLLDIG_MFD_MASK GENMASK(7, 0)
24d37010a3SWen He #define PLLDIG_RFDPHI1_MASK GENMASK(30, 25)
25d37010a3SWen He #define PLLDIG_REG_PLLFM 0x2c
26d37010a3SWen He #define PLLDIG_SSCGBYP_ENABLE BIT(30)
27d37010a3SWen He #define PLLDIG_REG_PLLFD 0x30
28d37010a3SWen He #define PLLDIG_FDEN BIT(30)
29d37010a3SWen He #define PLLDIG_FRAC_MASK GENMASK(15, 0)
30d37010a3SWen He #define PLLDIG_REG_PLLCAL1 0x38
31d37010a3SWen He #define PLLDIG_REG_PLLCAL2 0x3c
32d37010a3SWen He
33d37010a3SWen He /* Range of the VCO frequencies, in Hz */
34d37010a3SWen He #define PLLDIG_MIN_VCO_FREQ 650000000
35d37010a3SWen He #define PLLDIG_MAX_VCO_FREQ 1300000000
36d37010a3SWen He
37d37010a3SWen He /* Range of the output frequencies, in Hz */
380d152f2dSStephen Boyd #define PHI1_MIN_FREQ 27000000UL
390d152f2dSStephen Boyd #define PHI1_MAX_FREQ 600000000UL
40d37010a3SWen He
41d37010a3SWen He /* Maximum value of the reduced frequency divider */
42d37010a3SWen He #define MAX_RFDPHI1 63UL
43d37010a3SWen He
44d37010a3SWen He /* Best value of multiplication factor divider */
45d37010a3SWen He #define PLLDIG_DEFAULT_MFD 44
46d37010a3SWen He
47d37010a3SWen He /*
48d37010a3SWen He * Denominator part of the fractional part of the
49d37010a3SWen He * loop multiplication factor.
50d37010a3SWen He */
51d37010a3SWen He #define MFDEN 20480
52d37010a3SWen He
53d37010a3SWen He static const struct clk_parent_data parent_data[] = {
54d37010a3SWen He { .index = 0 },
55d37010a3SWen He };
56d37010a3SWen He
57d37010a3SWen He struct clk_plldig {
58d37010a3SWen He struct clk_hw hw;
59d37010a3SWen He void __iomem *regs;
60d37010a3SWen He unsigned int vco_freq;
61d37010a3SWen He };
62d37010a3SWen He
63d37010a3SWen He #define to_clk_plldig(_hw) container_of(_hw, struct clk_plldig, hw)
64d37010a3SWen He
plldig_enable(struct clk_hw * hw)65d37010a3SWen He static int plldig_enable(struct clk_hw *hw)
66d37010a3SWen He {
67d37010a3SWen He struct clk_plldig *data = to_clk_plldig(hw);
68d37010a3SWen He u32 val;
69d37010a3SWen He
70d37010a3SWen He val = readl(data->regs + PLLDIG_REG_PLLFM);
71d37010a3SWen He /*
72d37010a3SWen He * Use Bypass mode with PLL off by default, the frequency overshoot
73d37010a3SWen He * detector output was disable. SSCG Bypass mode should be enable.
74d37010a3SWen He */
75d37010a3SWen He val |= PLLDIG_SSCGBYP_ENABLE;
76d37010a3SWen He writel(val, data->regs + PLLDIG_REG_PLLFM);
77d37010a3SWen He
78d37010a3SWen He return 0;
79d37010a3SWen He }
80d37010a3SWen He
plldig_disable(struct clk_hw * hw)81d37010a3SWen He static void plldig_disable(struct clk_hw *hw)
82d37010a3SWen He {
83d37010a3SWen He struct clk_plldig *data = to_clk_plldig(hw);
84d37010a3SWen He u32 val;
85d37010a3SWen He
86d37010a3SWen He val = readl(data->regs + PLLDIG_REG_PLLFM);
87d37010a3SWen He
88d37010a3SWen He val &= ~PLLDIG_SSCGBYP_ENABLE;
89d37010a3SWen He val |= FIELD_PREP(PLLDIG_SSCGBYP_ENABLE, 0x0);
90d37010a3SWen He
91d37010a3SWen He writel(val, data->regs + PLLDIG_REG_PLLFM);
92d37010a3SWen He }
93d37010a3SWen He
plldig_is_enabled(struct clk_hw * hw)94d37010a3SWen He static int plldig_is_enabled(struct clk_hw *hw)
95d37010a3SWen He {
96d37010a3SWen He struct clk_plldig *data = to_clk_plldig(hw);
97d37010a3SWen He
98d37010a3SWen He return readl(data->regs + PLLDIG_REG_PLLFM) &
99d37010a3SWen He PLLDIG_SSCGBYP_ENABLE;
100d37010a3SWen He }
101d37010a3SWen He
plldig_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)102d37010a3SWen He static unsigned long plldig_recalc_rate(struct clk_hw *hw,
103d37010a3SWen He unsigned long parent_rate)
104d37010a3SWen He {
105d37010a3SWen He struct clk_plldig *data = to_clk_plldig(hw);
106d37010a3SWen He u32 val, rfdphi1;
107d37010a3SWen He
108d37010a3SWen He val = readl(data->regs + PLLDIG_REG_PLLDV);
109d37010a3SWen He
110d37010a3SWen He /* Check if PLL is bypassed */
111d37010a3SWen He if (val & PLLDIG_SSCGBYP_ENABLE)
112d37010a3SWen He return parent_rate;
113d37010a3SWen He
114d37010a3SWen He rfdphi1 = FIELD_GET(PLLDIG_RFDPHI1_MASK, val);
115d37010a3SWen He
116d37010a3SWen He /*
117d37010a3SWen He * If RFDPHI1 has a value of 1 the VCO frequency is also divided by
118d37010a3SWen He * one.
119d37010a3SWen He */
120d37010a3SWen He if (!rfdphi1)
121d37010a3SWen He rfdphi1 = 1;
122d37010a3SWen He
123d37010a3SWen He return DIV_ROUND_UP(data->vco_freq, rfdphi1);
124d37010a3SWen He }
125d37010a3SWen He
plldig_calc_target_div(unsigned long vco_freq,unsigned long target_rate)126d37010a3SWen He static unsigned long plldig_calc_target_div(unsigned long vco_freq,
127d37010a3SWen He unsigned long target_rate)
128d37010a3SWen He {
129d37010a3SWen He unsigned long div;
130d37010a3SWen He
131d37010a3SWen He div = DIV_ROUND_CLOSEST(vco_freq, target_rate);
132d37010a3SWen He div = clamp(div, 1UL, MAX_RFDPHI1);
133d37010a3SWen He
134d37010a3SWen He return div;
135d37010a3SWen He }
136d37010a3SWen He
plldig_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)137d37010a3SWen He static int plldig_determine_rate(struct clk_hw *hw,
138d37010a3SWen He struct clk_rate_request *req)
139d37010a3SWen He {
140d37010a3SWen He struct clk_plldig *data = to_clk_plldig(hw);
141d37010a3SWen He unsigned int div;
142d37010a3SWen He
143d37010a3SWen He req->rate = clamp(req->rate, PHI1_MIN_FREQ, PHI1_MAX_FREQ);
144d37010a3SWen He div = plldig_calc_target_div(data->vco_freq, req->rate);
145d37010a3SWen He req->rate = DIV_ROUND_UP(data->vco_freq, div);
146d37010a3SWen He
147d37010a3SWen He return 0;
148d37010a3SWen He }
149d37010a3SWen He
plldig_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)150d37010a3SWen He static int plldig_set_rate(struct clk_hw *hw, unsigned long rate,
151d37010a3SWen He unsigned long parent_rate)
152d37010a3SWen He {
153d37010a3SWen He struct clk_plldig *data = to_clk_plldig(hw);
154d37010a3SWen He unsigned int val, cond;
155d37010a3SWen He unsigned int rfdphi1;
156d37010a3SWen He
157d37010a3SWen He rate = clamp(rate, PHI1_MIN_FREQ, PHI1_MAX_FREQ);
158d37010a3SWen He rfdphi1 = plldig_calc_target_div(data->vco_freq, rate);
159d37010a3SWen He
160d37010a3SWen He /* update the divider value */
161d37010a3SWen He val = readl(data->regs + PLLDIG_REG_PLLDV);
162d37010a3SWen He val &= ~PLLDIG_RFDPHI1_MASK;
163d37010a3SWen He val |= FIELD_PREP(PLLDIG_RFDPHI1_MASK, rfdphi1);
164d37010a3SWen He writel(val, data->regs + PLLDIG_REG_PLLDV);
165d37010a3SWen He
166d37010a3SWen He /* waiting for old lock state to clear */
167d37010a3SWen He udelay(200);
168d37010a3SWen He
169d37010a3SWen He /* Wait until PLL is locked or timeout */
170d37010a3SWen He return readl_poll_timeout_atomic(data->regs + PLLDIG_REG_PLLSR, cond,
171d37010a3SWen He cond & PLLDIG_LOCK_MASK, 0,
172d37010a3SWen He USEC_PER_MSEC);
173d37010a3SWen He }
174d37010a3SWen He
175d37010a3SWen He static const struct clk_ops plldig_clk_ops = {
176d37010a3SWen He .enable = plldig_enable,
177d37010a3SWen He .disable = plldig_disable,
178d37010a3SWen He .is_enabled = plldig_is_enabled,
179d37010a3SWen He .recalc_rate = plldig_recalc_rate,
180d37010a3SWen He .determine_rate = plldig_determine_rate,
181d37010a3SWen He .set_rate = plldig_set_rate,
182d37010a3SWen He };
183d37010a3SWen He
plldig_init(struct clk_hw * hw)184d37010a3SWen He static int plldig_init(struct clk_hw *hw)
185d37010a3SWen He {
186d37010a3SWen He struct clk_plldig *data = to_clk_plldig(hw);
187d37010a3SWen He struct clk_hw *parent = clk_hw_get_parent(hw);
188*73cb3106SColin Ian King unsigned long parent_rate;
189d37010a3SWen He unsigned long val;
190d37010a3SWen He unsigned long long lltmp;
191d37010a3SWen He unsigned int mfd, fracdiv = 0;
192d37010a3SWen He
193d37010a3SWen He if (!parent)
194d37010a3SWen He return -EINVAL;
195d37010a3SWen He
196*73cb3106SColin Ian King parent_rate = clk_hw_get_rate(parent);
197*73cb3106SColin Ian King
198d37010a3SWen He if (data->vco_freq) {
199d37010a3SWen He mfd = data->vco_freq / parent_rate;
200d37010a3SWen He lltmp = data->vco_freq % parent_rate;
201d37010a3SWen He lltmp *= MFDEN;
202d37010a3SWen He do_div(lltmp, parent_rate);
203d37010a3SWen He fracdiv = lltmp;
204d37010a3SWen He } else {
205d37010a3SWen He mfd = PLLDIG_DEFAULT_MFD;
206d37010a3SWen He data->vco_freq = parent_rate * mfd;
207d37010a3SWen He }
208d37010a3SWen He
209d37010a3SWen He val = FIELD_PREP(PLLDIG_MFD_MASK, mfd);
210d37010a3SWen He writel(val, data->regs + PLLDIG_REG_PLLDV);
211d37010a3SWen He
212d37010a3SWen He /* Enable fractional divider */
213d37010a3SWen He if (fracdiv) {
214d37010a3SWen He val = FIELD_PREP(PLLDIG_FRAC_MASK, fracdiv);
215d37010a3SWen He val |= PLLDIG_FDEN;
216d37010a3SWen He writel(val, data->regs + PLLDIG_REG_PLLFD);
217d37010a3SWen He }
218d37010a3SWen He
219d37010a3SWen He return 0;
220d37010a3SWen He }
221d37010a3SWen He
plldig_clk_probe(struct platform_device * pdev)222d37010a3SWen He static int plldig_clk_probe(struct platform_device *pdev)
223d37010a3SWen He {
224d37010a3SWen He struct clk_plldig *data;
225d37010a3SWen He struct device *dev = &pdev->dev;
226d37010a3SWen He int ret;
227d37010a3SWen He
228d37010a3SWen He data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
229d37010a3SWen He if (!data)
230d37010a3SWen He return -ENOMEM;
231d37010a3SWen He
232d37010a3SWen He data->regs = devm_platform_ioremap_resource(pdev, 0);
233d37010a3SWen He if (IS_ERR(data->regs))
234d37010a3SWen He return PTR_ERR(data->regs);
235d37010a3SWen He
236d37010a3SWen He data->hw.init = CLK_HW_INIT_PARENTS_DATA("dpclk",
237d37010a3SWen He parent_data,
238d37010a3SWen He &plldig_clk_ops,
239d37010a3SWen He 0);
240d37010a3SWen He
241d37010a3SWen He ret = devm_clk_hw_register(dev, &data->hw);
242d37010a3SWen He if (ret) {
243d37010a3SWen He dev_err(dev, "failed to register %s clock\n",
244d37010a3SWen He dev->of_node->name);
245d37010a3SWen He return ret;
246d37010a3SWen He }
247d37010a3SWen He
248d37010a3SWen He ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
249d37010a3SWen He &data->hw);
250d37010a3SWen He if (ret) {
251d37010a3SWen He dev_err(dev, "unable to add clk provider\n");
252d37010a3SWen He return ret;
253d37010a3SWen He }
254d37010a3SWen He
255d37010a3SWen He /*
256d37010a3SWen He * The frequency of the VCO cannot be changed during runtime.
257d37010a3SWen He * Therefore, let the user specify a desired frequency.
258d37010a3SWen He */
259d37010a3SWen He if (!of_property_read_u32(dev->of_node, "fsl,vco-hz",
260d37010a3SWen He &data->vco_freq)) {
261d37010a3SWen He if (data->vco_freq < PLLDIG_MIN_VCO_FREQ ||
262d37010a3SWen He data->vco_freq > PLLDIG_MAX_VCO_FREQ)
263d37010a3SWen He return -EINVAL;
264d37010a3SWen He }
265d37010a3SWen He
266d37010a3SWen He return plldig_init(&data->hw);
267d37010a3SWen He }
268d37010a3SWen He
269d37010a3SWen He static const struct of_device_id plldig_clk_id[] = {
270d37010a3SWen He { .compatible = "fsl,ls1028a-plldig" },
271d37010a3SWen He { }
272d37010a3SWen He };
273d37010a3SWen He MODULE_DEVICE_TABLE(of, plldig_clk_id);
274d37010a3SWen He
275d37010a3SWen He static struct platform_driver plldig_clk_driver = {
276d37010a3SWen He .driver = {
277d37010a3SWen He .name = "plldig-clock",
278d37010a3SWen He .of_match_table = plldig_clk_id,
279d37010a3SWen He },
280d37010a3SWen He .probe = plldig_clk_probe,
281d37010a3SWen He };
282d37010a3SWen He module_platform_driver(plldig_clk_driver);
283d37010a3SWen He
284d37010a3SWen He MODULE_LICENSE("GPL v2");
285d37010a3SWen He MODULE_AUTHOR("Wen He <wen.he_1@nxp.com>");
286d37010a3SWen He MODULE_DESCRIPTION("LS1028A Display output interface pixel clock driver");
287