xref: /linux/drivers/clk/clk-twl6040.c (revision 7e37deb7fae8437c0487d9fc3f13c7415770efd7)
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 
29*7e37deb7SPeter Ujfalusi struct twl6040_pdmclk {
30f9f8c043SPeter Ujfalusi 	struct twl6040 *twl6040;
31f9f8c043SPeter Ujfalusi 	struct device *dev;
32*7e37deb7SPeter Ujfalusi 	struct clk_hw pdmclk_hw;
33f9f8c043SPeter Ujfalusi 	struct clk *clk;
34f9f8c043SPeter Ujfalusi 	int enabled;
35f9f8c043SPeter Ujfalusi };
36f9f8c043SPeter Ujfalusi 
37*7e37deb7SPeter Ujfalusi static int twl6040_pdmclk_is_prepared(struct clk_hw *hw)
38f9f8c043SPeter Ujfalusi {
39*7e37deb7SPeter Ujfalusi 	struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
40*7e37deb7SPeter Ujfalusi 						     pdmclk_hw);
41*7e37deb7SPeter Ujfalusi 
42*7e37deb7SPeter Ujfalusi 	return pdmclk->enabled;
43f9f8c043SPeter Ujfalusi }
44f9f8c043SPeter Ujfalusi 
45*7e37deb7SPeter Ujfalusi static int twl6040_pdmclk_prepare(struct clk_hw *hw)
46f9f8c043SPeter Ujfalusi {
47*7e37deb7SPeter Ujfalusi 	struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
48*7e37deb7SPeter Ujfalusi 						     pdmclk_hw);
49f9f8c043SPeter Ujfalusi 	int ret;
50f9f8c043SPeter Ujfalusi 
51*7e37deb7SPeter Ujfalusi 	ret = twl6040_power(pdmclk->twl6040, 1);
52f9f8c043SPeter Ujfalusi 	if (!ret)
53*7e37deb7SPeter Ujfalusi 		pdmclk->enabled = 1;
54f9f8c043SPeter Ujfalusi 
55f9f8c043SPeter Ujfalusi 	return ret;
56f9f8c043SPeter Ujfalusi }
57f9f8c043SPeter Ujfalusi 
58*7e37deb7SPeter Ujfalusi static void twl6040_pdmclk_unprepare(struct clk_hw *hw)
59f9f8c043SPeter Ujfalusi {
60*7e37deb7SPeter Ujfalusi 	struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
61*7e37deb7SPeter Ujfalusi 						     pdmclk_hw);
62f9f8c043SPeter Ujfalusi 	int ret;
63f9f8c043SPeter Ujfalusi 
64*7e37deb7SPeter Ujfalusi 	ret = twl6040_power(pdmclk->twl6040, 0);
65f9f8c043SPeter Ujfalusi 	if (!ret)
66*7e37deb7SPeter Ujfalusi 		pdmclk->enabled = 0;
67*7e37deb7SPeter Ujfalusi 
68f9f8c043SPeter Ujfalusi }
69f9f8c043SPeter Ujfalusi 
70*7e37deb7SPeter Ujfalusi static unsigned long twl6040_pdmclk_recalc_rate(struct clk_hw *hw,
71*7e37deb7SPeter Ujfalusi 						unsigned long parent_rate)
72*7e37deb7SPeter Ujfalusi {
73*7e37deb7SPeter Ujfalusi 	struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
74*7e37deb7SPeter Ujfalusi 						     pdmclk_hw);
75*7e37deb7SPeter Ujfalusi 
76*7e37deb7SPeter Ujfalusi 	return twl6040_get_sysclk(pdmclk->twl6040);
77*7e37deb7SPeter Ujfalusi }
78*7e37deb7SPeter Ujfalusi 
79*7e37deb7SPeter Ujfalusi static const struct clk_ops twl6040_pdmclk_ops = {
80*7e37deb7SPeter Ujfalusi 	.is_prepared = twl6040_pdmclk_is_prepared,
81*7e37deb7SPeter Ujfalusi 	.prepare = twl6040_pdmclk_prepare,
82*7e37deb7SPeter Ujfalusi 	.unprepare = twl6040_pdmclk_unprepare,
83*7e37deb7SPeter Ujfalusi 	.recalc_rate = twl6040_pdmclk_recalc_rate,
84f9f8c043SPeter Ujfalusi };
85f9f8c043SPeter Ujfalusi 
86*7e37deb7SPeter Ujfalusi static struct clk_init_data twl6040_pdmclk_init = {
87*7e37deb7SPeter Ujfalusi 	.name = "pdmclk",
88*7e37deb7SPeter Ujfalusi 	.ops = &twl6040_pdmclk_ops,
89*7e37deb7SPeter Ujfalusi 	.flags = CLK_GET_RATE_NOCACHE,
90f9f8c043SPeter Ujfalusi };
91f9f8c043SPeter Ujfalusi 
92*7e37deb7SPeter Ujfalusi static int twl6040_pdmclk_probe(struct platform_device *pdev)
93f9f8c043SPeter Ujfalusi {
94f9f8c043SPeter Ujfalusi 	struct twl6040 *twl6040 = dev_get_drvdata(pdev->dev.parent);
95*7e37deb7SPeter Ujfalusi 	struct twl6040_pdmclk *clkdata;
96f9f8c043SPeter Ujfalusi 
97f9f8c043SPeter Ujfalusi 	clkdata = devm_kzalloc(&pdev->dev, sizeof(*clkdata), GFP_KERNEL);
98f9f8c043SPeter Ujfalusi 	if (!clkdata)
99f9f8c043SPeter Ujfalusi 		return -ENOMEM;
100f9f8c043SPeter Ujfalusi 
101f9f8c043SPeter Ujfalusi 	clkdata->dev = &pdev->dev;
102f9f8c043SPeter Ujfalusi 	clkdata->twl6040 = twl6040;
103f9f8c043SPeter Ujfalusi 
104*7e37deb7SPeter Ujfalusi 	clkdata->pdmclk_hw.init = &twl6040_pdmclk_init;
105*7e37deb7SPeter Ujfalusi 	clkdata->clk = devm_clk_register(&pdev->dev, &clkdata->pdmclk_hw);
106ddc07ef9SWei Yongjun 	if (IS_ERR(clkdata->clk))
107ddc07ef9SWei Yongjun 		return PTR_ERR(clkdata->clk);
108f9f8c043SPeter Ujfalusi 
109c0431037SJingoo Han 	platform_set_drvdata(pdev, clkdata);
110f9f8c043SPeter Ujfalusi 
111994deaaeSPeter Ujfalusi 	return of_clk_add_provider(pdev->dev.parent->of_node,
112994deaaeSPeter Ujfalusi 				   of_clk_src_simple_get, clkdata->clk);
113f9f8c043SPeter Ujfalusi }
114f9f8c043SPeter Ujfalusi 
115*7e37deb7SPeter Ujfalusi static struct platform_driver twl6040_pdmclk_driver = {
116f9f8c043SPeter Ujfalusi 	.driver = {
117*7e37deb7SPeter Ujfalusi 		.name = "twl6040-pdmclk",
118f9f8c043SPeter Ujfalusi 	},
119*7e37deb7SPeter Ujfalusi 	.probe = twl6040_pdmclk_probe,
120f9f8c043SPeter Ujfalusi };
121f9f8c043SPeter Ujfalusi 
122*7e37deb7SPeter Ujfalusi module_platform_driver(twl6040_pdmclk_driver);
123f9f8c043SPeter Ujfalusi 
124f9f8c043SPeter Ujfalusi MODULE_DESCRIPTION("TWL6040 clock driver for McPDM functional clock");
125f9f8c043SPeter Ujfalusi MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
126*7e37deb7SPeter Ujfalusi MODULE_ALIAS("platform:twl6040-pdmclk");
127f9f8c043SPeter Ujfalusi MODULE_LICENSE("GPL");
128