1ff5f87cbSHans de Goede // SPDX-License-Identifier: GPL-2.0
2ff5f87cbSHans de Goede /*
3ff5f87cbSHans de Goede * Clock driver for TPS68470 PMIC
4ff5f87cbSHans de Goede *
5ff5f87cbSHans de Goede * Copyright (c) 2021 Red Hat Inc.
6ff5f87cbSHans de Goede * Copyright (C) 2018 Intel Corporation
7ff5f87cbSHans de Goede *
8ff5f87cbSHans de Goede * Authors:
9ff5f87cbSHans de Goede * Hans de Goede <hdegoede@redhat.com>
10ff5f87cbSHans de Goede * Zaikuo Wang <zaikuo.wang@intel.com>
11ff5f87cbSHans de Goede * Tianshu Qiu <tian.shu.qiu@intel.com>
12ff5f87cbSHans de Goede * Jian Xu Zheng <jian.xu.zheng@intel.com>
13ff5f87cbSHans de Goede * Yuning Pu <yuning.pu@intel.com>
14ff5f87cbSHans de Goede * Antti Laakso <antti.laakso@intel.com>
15ff5f87cbSHans de Goede */
16ff5f87cbSHans de Goede
17ff5f87cbSHans de Goede #include <linux/clk-provider.h>
18ff5f87cbSHans de Goede #include <linux/clkdev.h>
19ff5f87cbSHans de Goede #include <linux/kernel.h>
20ff5f87cbSHans de Goede #include <linux/mfd/tps68470.h>
21ff5f87cbSHans de Goede #include <linux/module.h>
22ff5f87cbSHans de Goede #include <linux/platform_device.h>
23ff5f87cbSHans de Goede #include <linux/platform_data/tps68470.h>
24ff5f87cbSHans de Goede #include <linux/regmap.h>
25ff5f87cbSHans de Goede
26ff5f87cbSHans de Goede #define TPS68470_CLK_NAME "tps68470-clk"
27ff5f87cbSHans de Goede
28ff5f87cbSHans de Goede #define to_tps68470_clkdata(clkd) \
29ff5f87cbSHans de Goede container_of(clkd, struct tps68470_clkdata, clkout_hw)
30ff5f87cbSHans de Goede
31ff5f87cbSHans de Goede static struct tps68470_clkout_freqs {
32ff5f87cbSHans de Goede unsigned long freq;
33ff5f87cbSHans de Goede unsigned int xtaldiv;
34ff5f87cbSHans de Goede unsigned int plldiv;
35ff5f87cbSHans de Goede unsigned int postdiv;
36ff5f87cbSHans de Goede unsigned int buckdiv;
37ff5f87cbSHans de Goede unsigned int boostdiv;
38ff5f87cbSHans de Goede } clk_freqs[] = {
39ff5f87cbSHans de Goede /*
40ff5f87cbSHans de Goede * The PLL is used to multiply the crystal oscillator
41ff5f87cbSHans de Goede * frequency range of 3 MHz to 27 MHz by a programmable
42ff5f87cbSHans de Goede * factor of F = (M/N)*(1/P) such that the output
43ff5f87cbSHans de Goede * available at the HCLK_A or HCLK_B pins are in the range
44ff5f87cbSHans de Goede * of 4 MHz to 64 MHz in increments of 0.1 MHz.
45ff5f87cbSHans de Goede *
46ff5f87cbSHans de Goede * hclk_# = osc_in * (((plldiv*2)+320) / (xtaldiv+30)) * (1 / 2^postdiv)
47ff5f87cbSHans de Goede *
48ff5f87cbSHans de Goede * PLL_REF_CLK should be as close as possible to 100kHz
49ff5f87cbSHans de Goede * PLL_REF_CLK = input clk / XTALDIV[7:0] + 30)
50ff5f87cbSHans de Goede *
51ff5f87cbSHans de Goede * PLL_VCO_CLK = (PLL_REF_CLK * (plldiv*2 + 320))
52ff5f87cbSHans de Goede *
53ff5f87cbSHans de Goede * BOOST should be as close as possible to 2Mhz
54ff5f87cbSHans de Goede * BOOST = PLL_VCO_CLK / (BOOSTDIV[4:0] + 16) *
55ff5f87cbSHans de Goede *
56ff5f87cbSHans de Goede * BUCK should be as close as possible to 5.2Mhz
57ff5f87cbSHans de Goede * BUCK = PLL_VCO_CLK / (BUCKDIV[3:0] + 5)
58ff5f87cbSHans de Goede *
59ff5f87cbSHans de Goede * osc_in xtaldiv plldiv postdiv hclk_#
60ff5f87cbSHans de Goede * 20Mhz 170 32 1 19.2Mhz
61ff5f87cbSHans de Goede * 20Mhz 170 40 1 20Mhz
62ff5f87cbSHans de Goede * 20Mhz 170 80 1 24Mhz
63ff5f87cbSHans de Goede */
64ff5f87cbSHans de Goede { 19200000, 170, 32, 1, 2, 3 },
65ff5f87cbSHans de Goede { 20000000, 170, 40, 1, 3, 4 },
66ff5f87cbSHans de Goede { 24000000, 170, 80, 1, 4, 8 },
67ff5f87cbSHans de Goede };
68ff5f87cbSHans de Goede
69ff5f87cbSHans de Goede struct tps68470_clkdata {
70ff5f87cbSHans de Goede struct clk_hw clkout_hw;
71ff5f87cbSHans de Goede struct regmap *regmap;
72ff5f87cbSHans de Goede unsigned long rate;
73ff5f87cbSHans de Goede };
74ff5f87cbSHans de Goede
tps68470_clk_is_prepared(struct clk_hw * hw)75ff5f87cbSHans de Goede static int tps68470_clk_is_prepared(struct clk_hw *hw)
76ff5f87cbSHans de Goede {
77ff5f87cbSHans de Goede struct tps68470_clkdata *clkdata = to_tps68470_clkdata(hw);
78ff5f87cbSHans de Goede int val;
79ff5f87cbSHans de Goede
80ff5f87cbSHans de Goede if (regmap_read(clkdata->regmap, TPS68470_REG_PLLCTL, &val))
81ff5f87cbSHans de Goede return 0;
82ff5f87cbSHans de Goede
83ff5f87cbSHans de Goede return val & TPS68470_PLL_EN_MASK;
84ff5f87cbSHans de Goede }
85ff5f87cbSHans de Goede
tps68470_clk_prepare(struct clk_hw * hw)86ff5f87cbSHans de Goede static int tps68470_clk_prepare(struct clk_hw *hw)
87ff5f87cbSHans de Goede {
88ff5f87cbSHans de Goede struct tps68470_clkdata *clkdata = to_tps68470_clkdata(hw);
89ff5f87cbSHans de Goede
90ff5f87cbSHans de Goede regmap_write(clkdata->regmap, TPS68470_REG_CLKCFG1,
91ff5f87cbSHans de Goede (TPS68470_PLL_OUTPUT_ENABLE << TPS68470_OUTPUT_A_SHIFT) |
92ff5f87cbSHans de Goede (TPS68470_PLL_OUTPUT_ENABLE << TPS68470_OUTPUT_B_SHIFT));
93ff5f87cbSHans de Goede
94ff5f87cbSHans de Goede regmap_update_bits(clkdata->regmap, TPS68470_REG_PLLCTL,
95ff5f87cbSHans de Goede TPS68470_PLL_EN_MASK, TPS68470_PLL_EN_MASK);
96ff5f87cbSHans de Goede
97ff5f87cbSHans de Goede /*
98ff5f87cbSHans de Goede * The PLLCTL reg lock bit is set by the PMIC after approx. 4ms and
99ff5f87cbSHans de Goede * does not indicate a true lock, so just wait 4 ms.
100ff5f87cbSHans de Goede */
101ff5f87cbSHans de Goede usleep_range(4000, 5000);
102ff5f87cbSHans de Goede
103ff5f87cbSHans de Goede return 0;
104ff5f87cbSHans de Goede }
105ff5f87cbSHans de Goede
tps68470_clk_unprepare(struct clk_hw * hw)106ff5f87cbSHans de Goede static void tps68470_clk_unprepare(struct clk_hw *hw)
107ff5f87cbSHans de Goede {
108ff5f87cbSHans de Goede struct tps68470_clkdata *clkdata = to_tps68470_clkdata(hw);
109ff5f87cbSHans de Goede
110ff5f87cbSHans de Goede /* Disable clock first ... */
111ff5f87cbSHans de Goede regmap_update_bits(clkdata->regmap, TPS68470_REG_PLLCTL, TPS68470_PLL_EN_MASK, 0);
112ff5f87cbSHans de Goede
113ff5f87cbSHans de Goede /* ... and then tri-state the clock outputs. */
114ff5f87cbSHans de Goede regmap_write(clkdata->regmap, TPS68470_REG_CLKCFG1, 0);
115ff5f87cbSHans de Goede }
116ff5f87cbSHans de Goede
tps68470_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)117ff5f87cbSHans de Goede static unsigned long tps68470_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
118ff5f87cbSHans de Goede {
119ff5f87cbSHans de Goede struct tps68470_clkdata *clkdata = to_tps68470_clkdata(hw);
120ff5f87cbSHans de Goede
121ff5f87cbSHans de Goede return clkdata->rate;
122ff5f87cbSHans de Goede }
123ff5f87cbSHans de Goede
124ff5f87cbSHans de Goede /*
125ff5f87cbSHans de Goede * This returns the index of the clk_freqs[] cfg with the closest rate for
126ff5f87cbSHans de Goede * use in tps68470_clk_round_rate(). tps68470_clk_set_rate() checks that
127ff5f87cbSHans de Goede * the rate of the returned cfg is an exact match.
128ff5f87cbSHans de Goede */
tps68470_clk_cfg_lookup(unsigned long rate)129ff5f87cbSHans de Goede static unsigned int tps68470_clk_cfg_lookup(unsigned long rate)
130ff5f87cbSHans de Goede {
131ff5f87cbSHans de Goede long diff, best_diff = LONG_MAX;
132ff5f87cbSHans de Goede unsigned int i, best_idx = 0;
133ff5f87cbSHans de Goede
134ff5f87cbSHans de Goede for (i = 0; i < ARRAY_SIZE(clk_freqs); i++) {
135ff5f87cbSHans de Goede diff = clk_freqs[i].freq - rate;
136ff5f87cbSHans de Goede if (diff == 0)
137ff5f87cbSHans de Goede return i;
138ff5f87cbSHans de Goede
139ff5f87cbSHans de Goede diff = abs(diff);
140ff5f87cbSHans de Goede if (diff < best_diff) {
141ff5f87cbSHans de Goede best_diff = diff;
142ff5f87cbSHans de Goede best_idx = i;
143ff5f87cbSHans de Goede }
144ff5f87cbSHans de Goede }
145ff5f87cbSHans de Goede
146ff5f87cbSHans de Goede return best_idx;
147ff5f87cbSHans de Goede }
148ff5f87cbSHans de Goede
tps68470_clk_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)149ff5f87cbSHans de Goede static long tps68470_clk_round_rate(struct clk_hw *hw, unsigned long rate,
150ff5f87cbSHans de Goede unsigned long *parent_rate)
151ff5f87cbSHans de Goede {
152ff5f87cbSHans de Goede unsigned int idx = tps68470_clk_cfg_lookup(rate);
153ff5f87cbSHans de Goede
154ff5f87cbSHans de Goede return clk_freqs[idx].freq;
155ff5f87cbSHans de Goede }
156ff5f87cbSHans de Goede
tps68470_clk_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)157ff5f87cbSHans de Goede static int tps68470_clk_set_rate(struct clk_hw *hw, unsigned long rate,
158ff5f87cbSHans de Goede unsigned long parent_rate)
159ff5f87cbSHans de Goede {
160ff5f87cbSHans de Goede struct tps68470_clkdata *clkdata = to_tps68470_clkdata(hw);
161ff5f87cbSHans de Goede unsigned int idx = tps68470_clk_cfg_lookup(rate);
162ff5f87cbSHans de Goede
163ff5f87cbSHans de Goede if (rate != clk_freqs[idx].freq)
164ff5f87cbSHans de Goede return -EINVAL;
165ff5f87cbSHans de Goede
166ff5f87cbSHans de Goede regmap_write(clkdata->regmap, TPS68470_REG_BOOSTDIV, clk_freqs[idx].boostdiv);
167ff5f87cbSHans de Goede regmap_write(clkdata->regmap, TPS68470_REG_BUCKDIV, clk_freqs[idx].buckdiv);
168ff5f87cbSHans de Goede regmap_write(clkdata->regmap, TPS68470_REG_PLLSWR, TPS68470_PLLSWR_DEFAULT);
169ff5f87cbSHans de Goede regmap_write(clkdata->regmap, TPS68470_REG_XTALDIV, clk_freqs[idx].xtaldiv);
170ff5f87cbSHans de Goede regmap_write(clkdata->regmap, TPS68470_REG_PLLDIV, clk_freqs[idx].plldiv);
171ff5f87cbSHans de Goede regmap_write(clkdata->regmap, TPS68470_REG_POSTDIV, clk_freqs[idx].postdiv);
172ff5f87cbSHans de Goede regmap_write(clkdata->regmap, TPS68470_REG_POSTDIV2, clk_freqs[idx].postdiv);
173ff5f87cbSHans de Goede regmap_write(clkdata->regmap, TPS68470_REG_CLKCFG2, TPS68470_CLKCFG2_DRV_STR_2MA);
174ff5f87cbSHans de Goede
175ff5f87cbSHans de Goede regmap_write(clkdata->regmap, TPS68470_REG_PLLCTL,
176ff5f87cbSHans de Goede TPS68470_OSC_EXT_CAP_DEFAULT << TPS68470_OSC_EXT_CAP_SHIFT |
177ff5f87cbSHans de Goede TPS68470_CLK_SRC_XTAL << TPS68470_CLK_SRC_SHIFT);
178ff5f87cbSHans de Goede
179ff5f87cbSHans de Goede clkdata->rate = rate;
180ff5f87cbSHans de Goede
181ff5f87cbSHans de Goede return 0;
182ff5f87cbSHans de Goede }
183ff5f87cbSHans de Goede
184ff5f87cbSHans de Goede static const struct clk_ops tps68470_clk_ops = {
185ff5f87cbSHans de Goede .is_prepared = tps68470_clk_is_prepared,
186ff5f87cbSHans de Goede .prepare = tps68470_clk_prepare,
187ff5f87cbSHans de Goede .unprepare = tps68470_clk_unprepare,
188ff5f87cbSHans de Goede .recalc_rate = tps68470_clk_recalc_rate,
189ff5f87cbSHans de Goede .round_rate = tps68470_clk_round_rate,
190ff5f87cbSHans de Goede .set_rate = tps68470_clk_set_rate,
191ff5f87cbSHans de Goede };
192ff5f87cbSHans de Goede
tps68470_clk_probe(struct platform_device * pdev)193ff5f87cbSHans de Goede static int tps68470_clk_probe(struct platform_device *pdev)
194ff5f87cbSHans de Goede {
195ff5f87cbSHans de Goede struct tps68470_clk_platform_data *pdata = pdev->dev.platform_data;
196ff5f87cbSHans de Goede struct clk_init_data tps68470_clk_initdata = {
197ff5f87cbSHans de Goede .name = TPS68470_CLK_NAME,
198ff5f87cbSHans de Goede .ops = &tps68470_clk_ops,
199ff5f87cbSHans de Goede /* Changing the dividers when the PLL is on is not allowed */
200ff5f87cbSHans de Goede .flags = CLK_SET_RATE_GATE,
201ff5f87cbSHans de Goede };
202ff5f87cbSHans de Goede struct tps68470_clkdata *tps68470_clkdata;
203*43cf3697SDaniel Scally struct tps68470_clk_consumer *consumer;
204ff5f87cbSHans de Goede int ret;
205*43cf3697SDaniel Scally int i;
206ff5f87cbSHans de Goede
207ff5f87cbSHans de Goede tps68470_clkdata = devm_kzalloc(&pdev->dev, sizeof(*tps68470_clkdata),
208ff5f87cbSHans de Goede GFP_KERNEL);
209ff5f87cbSHans de Goede if (!tps68470_clkdata)
210ff5f87cbSHans de Goede return -ENOMEM;
211ff5f87cbSHans de Goede
212ff5f87cbSHans de Goede tps68470_clkdata->regmap = dev_get_drvdata(pdev->dev.parent);
213ff5f87cbSHans de Goede tps68470_clkdata->clkout_hw.init = &tps68470_clk_initdata;
214ff5f87cbSHans de Goede
215ff5f87cbSHans de Goede /* Set initial rate */
216ff5f87cbSHans de Goede tps68470_clk_set_rate(&tps68470_clkdata->clkout_hw, clk_freqs[0].freq, 0);
217ff5f87cbSHans de Goede
218ff5f87cbSHans de Goede ret = devm_clk_hw_register(&pdev->dev, &tps68470_clkdata->clkout_hw);
219ff5f87cbSHans de Goede if (ret)
220ff5f87cbSHans de Goede return ret;
221ff5f87cbSHans de Goede
222ff5f87cbSHans de Goede ret = devm_clk_hw_register_clkdev(&pdev->dev, &tps68470_clkdata->clkout_hw,
223ff5f87cbSHans de Goede TPS68470_CLK_NAME, NULL);
224ff5f87cbSHans de Goede if (ret)
225ff5f87cbSHans de Goede return ret;
226ff5f87cbSHans de Goede
227ff5f87cbSHans de Goede if (pdata) {
228*43cf3697SDaniel Scally for (i = 0; i < pdata->n_consumers; i++) {
229*43cf3697SDaniel Scally consumer = &pdata->consumers[i];
230ff5f87cbSHans de Goede ret = devm_clk_hw_register_clkdev(&pdev->dev,
231ff5f87cbSHans de Goede &tps68470_clkdata->clkout_hw,
232*43cf3697SDaniel Scally consumer->consumer_con_id,
233*43cf3697SDaniel Scally consumer->consumer_dev_name);
234*43cf3697SDaniel Scally }
235ff5f87cbSHans de Goede }
236ff5f87cbSHans de Goede
237ff5f87cbSHans de Goede return ret;
238ff5f87cbSHans de Goede }
239ff5f87cbSHans de Goede
240ff5f87cbSHans de Goede static struct platform_driver tps68470_clk_driver = {
241ff5f87cbSHans de Goede .driver = {
242ff5f87cbSHans de Goede .name = TPS68470_CLK_NAME,
243ff5f87cbSHans de Goede },
244ff5f87cbSHans de Goede .probe = tps68470_clk_probe,
245ff5f87cbSHans de Goede };
246ff5f87cbSHans de Goede
247ff5f87cbSHans de Goede /*
248ff5f87cbSHans de Goede * The ACPI tps68470 probe-ordering depends on the clk/gpio/regulator drivers
249ff5f87cbSHans de Goede * registering before the drivers for the camera-sensors which use them bind.
250ff5f87cbSHans de Goede * subsys_initcall() ensures this when the drivers are builtin.
251ff5f87cbSHans de Goede */
tps68470_clk_init(void)252ff5f87cbSHans de Goede static int __init tps68470_clk_init(void)
253ff5f87cbSHans de Goede {
254ff5f87cbSHans de Goede return platform_driver_register(&tps68470_clk_driver);
255ff5f87cbSHans de Goede }
256ff5f87cbSHans de Goede subsys_initcall(tps68470_clk_init);
257ff5f87cbSHans de Goede
tps68470_clk_exit(void)258ff5f87cbSHans de Goede static void __exit tps68470_clk_exit(void)
259ff5f87cbSHans de Goede {
260ff5f87cbSHans de Goede platform_driver_unregister(&tps68470_clk_driver);
261ff5f87cbSHans de Goede }
262ff5f87cbSHans de Goede module_exit(tps68470_clk_exit);
263ff5f87cbSHans de Goede
264ff5f87cbSHans de Goede MODULE_ALIAS("platform:tps68470-clk");
265ff5f87cbSHans de Goede MODULE_DESCRIPTION("clock driver for TPS68470 pmic");
266ff5f87cbSHans de Goede MODULE_LICENSE("GPL");
267