xref: /linux/drivers/clk/clk-twl6040.c (revision 5ae51d67aec95f6f9386aa8dd5db424964895575)
1f9f8c043SPeter Ujfalusi /*
2f9f8c043SPeter Ujfalusi * TWL6040 clock module driver for OMAP4 McPDM functional clock
3f9f8c043SPeter Ujfalusi *
4f9f8c043SPeter Ujfalusi * Copyright (C) 2012 Texas Instruments Inc.
5f9f8c043SPeter Ujfalusi * Peter Ujfalusi <peter.ujfalusi@ti.com>
6f9f8c043SPeter Ujfalusi *
7f9f8c043SPeter Ujfalusi * This program is free software; you can redistribute it and/or
8f9f8c043SPeter Ujfalusi * modify it under the terms of the GNU General Public License
9f9f8c043SPeter Ujfalusi * version 2 as published by the Free Software Foundation.
10f9f8c043SPeter Ujfalusi *
11f9f8c043SPeter Ujfalusi * This program is distributed in the hope that it will be useful, but
12f9f8c043SPeter Ujfalusi * WITHOUT ANY WARRANTY; without even the implied warranty of
13f9f8c043SPeter Ujfalusi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14f9f8c043SPeter Ujfalusi * General Public License for more details.
15f9f8c043SPeter Ujfalusi *
16f9f8c043SPeter Ujfalusi * You should have received a copy of the GNU General Public License
17f9f8c043SPeter Ujfalusi * along with this program; if not, write to the Free Software
18f9f8c043SPeter Ujfalusi * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19f9f8c043SPeter Ujfalusi * 02110-1301 USA
20f9f8c043SPeter Ujfalusi *
21f9f8c043SPeter Ujfalusi */
22f9f8c043SPeter Ujfalusi 
23f9f8c043SPeter Ujfalusi #include <linux/module.h>
24f9f8c043SPeter Ujfalusi #include <linux/slab.h>
25f9f8c043SPeter Ujfalusi #include <linux/platform_device.h>
26f9f8c043SPeter Ujfalusi #include <linux/mfd/twl6040.h>
27f9f8c043SPeter Ujfalusi #include <linux/clk-provider.h>
28f9f8c043SPeter Ujfalusi 
297e37deb7SPeter Ujfalusi struct twl6040_pdmclk {
30f9f8c043SPeter Ujfalusi 	struct twl6040 *twl6040;
31f9f8c043SPeter Ujfalusi 	struct device *dev;
327e37deb7SPeter Ujfalusi 	struct clk_hw pdmclk_hw;
33f9f8c043SPeter Ujfalusi 	int enabled;
34f9f8c043SPeter Ujfalusi };
35f9f8c043SPeter Ujfalusi 
367e37deb7SPeter Ujfalusi static int twl6040_pdmclk_is_prepared(struct clk_hw *hw)
37f9f8c043SPeter Ujfalusi {
387e37deb7SPeter Ujfalusi 	struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
397e37deb7SPeter Ujfalusi 						     pdmclk_hw);
407e37deb7SPeter Ujfalusi 
417e37deb7SPeter Ujfalusi 	return pdmclk->enabled;
42f9f8c043SPeter Ujfalusi }
43f9f8c043SPeter Ujfalusi 
44*5ae51d67STony Lindgren static int twl6040_pdmclk_reset_one_clock(struct twl6040_pdmclk *pdmclk,
45*5ae51d67STony Lindgren 					  unsigned int reg)
46*5ae51d67STony Lindgren {
47*5ae51d67STony Lindgren 	const u8 reset_mask = TWL6040_HPLLRST;	/* Same for HPPLL and LPPLL */
48*5ae51d67STony Lindgren 	int ret;
49*5ae51d67STony Lindgren 
50*5ae51d67STony Lindgren 	ret = twl6040_set_bits(pdmclk->twl6040, reg, reset_mask);
51*5ae51d67STony Lindgren 	if (ret < 0)
52*5ae51d67STony Lindgren 		return ret;
53*5ae51d67STony Lindgren 
54*5ae51d67STony Lindgren 	ret = twl6040_clear_bits(pdmclk->twl6040, reg, reset_mask);
55*5ae51d67STony Lindgren 	if (ret < 0)
56*5ae51d67STony Lindgren 		return ret;
57*5ae51d67STony Lindgren 
58*5ae51d67STony Lindgren 	return 0;
59*5ae51d67STony Lindgren }
60*5ae51d67STony Lindgren 
61*5ae51d67STony Lindgren /*
62*5ae51d67STony Lindgren  * TWL6040A2 Phoenix Audio IC erratum #6: "PDM Clock Generation Issue At
63*5ae51d67STony Lindgren  * Cold Temperature". This affects cold boot and deeper idle states it
64*5ae51d67STony Lindgren  * seems. The workaround consists of resetting HPPLL and LPPLL.
65*5ae51d67STony Lindgren  */
66*5ae51d67STony Lindgren static int twl6040_pdmclk_quirk_reset_clocks(struct twl6040_pdmclk *pdmclk)
67*5ae51d67STony Lindgren {
68*5ae51d67STony Lindgren 	int ret;
69*5ae51d67STony Lindgren 
70*5ae51d67STony Lindgren 	ret = twl6040_pdmclk_reset_one_clock(pdmclk, TWL6040_REG_HPPLLCTL);
71*5ae51d67STony Lindgren 	if (ret)
72*5ae51d67STony Lindgren 		return ret;
73*5ae51d67STony Lindgren 
74*5ae51d67STony Lindgren 	ret = twl6040_pdmclk_reset_one_clock(pdmclk, TWL6040_REG_LPPLLCTL);
75*5ae51d67STony Lindgren 	if (ret)
76*5ae51d67STony Lindgren 		return ret;
77*5ae51d67STony Lindgren 
78*5ae51d67STony Lindgren 	return 0;
79*5ae51d67STony Lindgren }
80*5ae51d67STony Lindgren 
817e37deb7SPeter Ujfalusi static int twl6040_pdmclk_prepare(struct clk_hw *hw)
82f9f8c043SPeter Ujfalusi {
837e37deb7SPeter Ujfalusi 	struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
847e37deb7SPeter Ujfalusi 						     pdmclk_hw);
85f9f8c043SPeter Ujfalusi 	int ret;
86f9f8c043SPeter Ujfalusi 
877e37deb7SPeter Ujfalusi 	ret = twl6040_power(pdmclk->twl6040, 1);
88*5ae51d67STony Lindgren 	if (ret)
89*5ae51d67STony Lindgren 		return ret;
90*5ae51d67STony Lindgren 
91*5ae51d67STony Lindgren 	ret = twl6040_pdmclk_quirk_reset_clocks(pdmclk);
92*5ae51d67STony Lindgren 	if (ret)
93*5ae51d67STony Lindgren 		goto out_err;
94*5ae51d67STony Lindgren 
957e37deb7SPeter Ujfalusi 	pdmclk->enabled = 1;
96f9f8c043SPeter Ujfalusi 
97*5ae51d67STony Lindgren 	return 0;
98*5ae51d67STony Lindgren 
99*5ae51d67STony Lindgren out_err:
100*5ae51d67STony Lindgren 	dev_err(pdmclk->dev, "%s: error %i\n", __func__, ret);
101*5ae51d67STony Lindgren 	twl6040_power(pdmclk->twl6040, 0);
102*5ae51d67STony Lindgren 
103f9f8c043SPeter Ujfalusi 	return ret;
104f9f8c043SPeter Ujfalusi }
105f9f8c043SPeter Ujfalusi 
1067e37deb7SPeter Ujfalusi static void twl6040_pdmclk_unprepare(struct clk_hw *hw)
107f9f8c043SPeter Ujfalusi {
1087e37deb7SPeter Ujfalusi 	struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
1097e37deb7SPeter Ujfalusi 						     pdmclk_hw);
110f9f8c043SPeter Ujfalusi 	int ret;
111f9f8c043SPeter Ujfalusi 
1127e37deb7SPeter Ujfalusi 	ret = twl6040_power(pdmclk->twl6040, 0);
113f9f8c043SPeter Ujfalusi 	if (!ret)
1147e37deb7SPeter Ujfalusi 		pdmclk->enabled = 0;
1157e37deb7SPeter Ujfalusi 
116f9f8c043SPeter Ujfalusi }
117f9f8c043SPeter Ujfalusi 
1187e37deb7SPeter Ujfalusi static unsigned long twl6040_pdmclk_recalc_rate(struct clk_hw *hw,
1197e37deb7SPeter Ujfalusi 						unsigned long parent_rate)
1207e37deb7SPeter Ujfalusi {
1217e37deb7SPeter Ujfalusi 	struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
1227e37deb7SPeter Ujfalusi 						     pdmclk_hw);
1237e37deb7SPeter Ujfalusi 
1247e37deb7SPeter Ujfalusi 	return twl6040_get_sysclk(pdmclk->twl6040);
1257e37deb7SPeter Ujfalusi }
1267e37deb7SPeter Ujfalusi 
1277e37deb7SPeter Ujfalusi static const struct clk_ops twl6040_pdmclk_ops = {
1287e37deb7SPeter Ujfalusi 	.is_prepared = twl6040_pdmclk_is_prepared,
1297e37deb7SPeter Ujfalusi 	.prepare = twl6040_pdmclk_prepare,
1307e37deb7SPeter Ujfalusi 	.unprepare = twl6040_pdmclk_unprepare,
1317e37deb7SPeter Ujfalusi 	.recalc_rate = twl6040_pdmclk_recalc_rate,
132f9f8c043SPeter Ujfalusi };
133f9f8c043SPeter Ujfalusi 
1340777066dSBhumika Goyal static const struct clk_init_data twl6040_pdmclk_init = {
1357e37deb7SPeter Ujfalusi 	.name = "pdmclk",
1367e37deb7SPeter Ujfalusi 	.ops = &twl6040_pdmclk_ops,
1377e37deb7SPeter Ujfalusi 	.flags = CLK_GET_RATE_NOCACHE,
138f9f8c043SPeter Ujfalusi };
139f9f8c043SPeter Ujfalusi 
1407e37deb7SPeter Ujfalusi static int twl6040_pdmclk_probe(struct platform_device *pdev)
141f9f8c043SPeter Ujfalusi {
142f9f8c043SPeter Ujfalusi 	struct twl6040 *twl6040 = dev_get_drvdata(pdev->dev.parent);
1437e37deb7SPeter Ujfalusi 	struct twl6040_pdmclk *clkdata;
144f5b3715eSStephen Boyd 	int ret;
145f9f8c043SPeter Ujfalusi 
146f9f8c043SPeter Ujfalusi 	clkdata = devm_kzalloc(&pdev->dev, sizeof(*clkdata), GFP_KERNEL);
147f9f8c043SPeter Ujfalusi 	if (!clkdata)
148f9f8c043SPeter Ujfalusi 		return -ENOMEM;
149f9f8c043SPeter Ujfalusi 
150f9f8c043SPeter Ujfalusi 	clkdata->dev = &pdev->dev;
151f9f8c043SPeter Ujfalusi 	clkdata->twl6040 = twl6040;
152f9f8c043SPeter Ujfalusi 
1537e37deb7SPeter Ujfalusi 	clkdata->pdmclk_hw.init = &twl6040_pdmclk_init;
154f5b3715eSStephen Boyd 	ret = devm_clk_hw_register(&pdev->dev, &clkdata->pdmclk_hw);
155f5b3715eSStephen Boyd 	if (ret)
156f5b3715eSStephen Boyd 		return ret;
157f9f8c043SPeter Ujfalusi 
158c0431037SJingoo Han 	platform_set_drvdata(pdev, clkdata);
159f9f8c043SPeter Ujfalusi 
160654dea6eSMatti Vaittinen 	return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_simple_get,
161f5b3715eSStephen Boyd 					   &clkdata->pdmclk_hw);
162f9f8c043SPeter Ujfalusi }
163f9f8c043SPeter Ujfalusi 
1647e37deb7SPeter Ujfalusi static struct platform_driver twl6040_pdmclk_driver = {
165f9f8c043SPeter Ujfalusi 	.driver = {
1667e37deb7SPeter Ujfalusi 		.name = "twl6040-pdmclk",
167f9f8c043SPeter Ujfalusi 	},
1687e37deb7SPeter Ujfalusi 	.probe = twl6040_pdmclk_probe,
169f9f8c043SPeter Ujfalusi };
170f9f8c043SPeter Ujfalusi 
1717e37deb7SPeter Ujfalusi module_platform_driver(twl6040_pdmclk_driver);
172f9f8c043SPeter Ujfalusi 
173f9f8c043SPeter Ujfalusi MODULE_DESCRIPTION("TWL6040 clock driver for McPDM functional clock");
174f9f8c043SPeter Ujfalusi MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
1757e37deb7SPeter Ujfalusi MODULE_ALIAS("platform:twl6040-pdmclk");
176f9f8c043SPeter Ujfalusi MODULE_LICENSE("GPL");
177