Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / clk / clk-twl6040.c
blobb8d1cfe5b0e1f50cd94686b331bb4cf2d66f3349
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * TWL6040 clock module driver for OMAP4 McPDM functional clock
5 * Copyright (C) 2012 Texas Instruments Inc.
6 * Peter Ujfalusi <peter.ujfalusi@ti.com>
7 */
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/platform_device.h>
12 #include <linux/mfd/twl6040.h>
13 #include <linux/clk-provider.h>
15 struct twl6040_pdmclk {
16 struct twl6040 *twl6040;
17 struct device *dev;
18 struct clk_hw pdmclk_hw;
19 int enabled;
22 static int twl6040_pdmclk_is_prepared(struct clk_hw *hw)
24 struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
25 pdmclk_hw);
27 return pdmclk->enabled;
30 static int twl6040_pdmclk_reset_one_clock(struct twl6040_pdmclk *pdmclk,
31 unsigned int reg)
33 const u8 reset_mask = TWL6040_HPLLRST; /* Same for HPPLL and LPPLL */
34 int ret;
36 ret = twl6040_set_bits(pdmclk->twl6040, reg, reset_mask);
37 if (ret < 0)
38 return ret;
40 ret = twl6040_clear_bits(pdmclk->twl6040, reg, reset_mask);
41 if (ret < 0)
42 return ret;
44 return 0;
48 * TWL6040A2 Phoenix Audio IC erratum #6: "PDM Clock Generation Issue At
49 * Cold Temperature". This affects cold boot and deeper idle states it
50 * seems. The workaround consists of resetting HPPLL and LPPLL.
52 static int twl6040_pdmclk_quirk_reset_clocks(struct twl6040_pdmclk *pdmclk)
54 int ret;
56 ret = twl6040_pdmclk_reset_one_clock(pdmclk, TWL6040_REG_HPPLLCTL);
57 if (ret)
58 return ret;
60 ret = twl6040_pdmclk_reset_one_clock(pdmclk, TWL6040_REG_LPPLLCTL);
61 if (ret)
62 return ret;
64 return 0;
67 static int twl6040_pdmclk_prepare(struct clk_hw *hw)
69 struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
70 pdmclk_hw);
71 int ret;
73 ret = twl6040_power(pdmclk->twl6040, 1);
74 if (ret)
75 return ret;
77 ret = twl6040_pdmclk_quirk_reset_clocks(pdmclk);
78 if (ret)
79 goto out_err;
81 pdmclk->enabled = 1;
83 return 0;
85 out_err:
86 dev_err(pdmclk->dev, "%s: error %i\n", __func__, ret);
87 twl6040_power(pdmclk->twl6040, 0);
89 return ret;
92 static void twl6040_pdmclk_unprepare(struct clk_hw *hw)
94 struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
95 pdmclk_hw);
96 int ret;
98 ret = twl6040_power(pdmclk->twl6040, 0);
99 if (!ret)
100 pdmclk->enabled = 0;
104 static unsigned long twl6040_pdmclk_recalc_rate(struct clk_hw *hw,
105 unsigned long parent_rate)
107 struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
108 pdmclk_hw);
110 return twl6040_get_sysclk(pdmclk->twl6040);
113 static const struct clk_ops twl6040_pdmclk_ops = {
114 .is_prepared = twl6040_pdmclk_is_prepared,
115 .prepare = twl6040_pdmclk_prepare,
116 .unprepare = twl6040_pdmclk_unprepare,
117 .recalc_rate = twl6040_pdmclk_recalc_rate,
120 static const struct clk_init_data twl6040_pdmclk_init = {
121 .name = "pdmclk",
122 .ops = &twl6040_pdmclk_ops,
123 .flags = CLK_GET_RATE_NOCACHE,
126 static int twl6040_pdmclk_probe(struct platform_device *pdev)
128 struct twl6040 *twl6040 = dev_get_drvdata(pdev->dev.parent);
129 struct twl6040_pdmclk *clkdata;
130 int ret;
132 clkdata = devm_kzalloc(&pdev->dev, sizeof(*clkdata), GFP_KERNEL);
133 if (!clkdata)
134 return -ENOMEM;
136 clkdata->dev = &pdev->dev;
137 clkdata->twl6040 = twl6040;
139 clkdata->pdmclk_hw.init = &twl6040_pdmclk_init;
140 ret = devm_clk_hw_register(&pdev->dev, &clkdata->pdmclk_hw);
141 if (ret)
142 return ret;
144 platform_set_drvdata(pdev, clkdata);
146 return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_simple_get,
147 &clkdata->pdmclk_hw);
150 static struct platform_driver twl6040_pdmclk_driver = {
151 .driver = {
152 .name = "twl6040-pdmclk",
154 .probe = twl6040_pdmclk_probe,
157 module_platform_driver(twl6040_pdmclk_driver);
159 MODULE_DESCRIPTION("TWL6040 clock driver for McPDM functional clock");
160 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
161 MODULE_ALIAS("platform:twl6040-pdmclk");
162 MODULE_LICENSE("GPL");