xref: /linux/drivers/clk/clk-twl6040.c (revision ddc07ef9499509200033be7e387feb6c83744a73)
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/clk.h>
24f9f8c043SPeter Ujfalusi #include <linux/module.h>
25f9f8c043SPeter Ujfalusi #include <linux/slab.h>
26f9f8c043SPeter Ujfalusi #include <linux/platform_device.h>
27f9f8c043SPeter Ujfalusi #include <linux/mfd/twl6040.h>
28f9f8c043SPeter Ujfalusi #include <linux/clk-provider.h>
29f9f8c043SPeter Ujfalusi 
30f9f8c043SPeter Ujfalusi struct twl6040_clk {
31f9f8c043SPeter Ujfalusi 	struct twl6040 *twl6040;
32f9f8c043SPeter Ujfalusi 	struct device *dev;
33f9f8c043SPeter Ujfalusi 	struct clk_hw mcpdm_fclk;
34f9f8c043SPeter Ujfalusi 	struct clk *clk;
35f9f8c043SPeter Ujfalusi 	int enabled;
36f9f8c043SPeter Ujfalusi };
37f9f8c043SPeter Ujfalusi 
38f9f8c043SPeter Ujfalusi static int twl6040_bitclk_is_enabled(struct clk_hw *hw)
39f9f8c043SPeter Ujfalusi {
40f9f8c043SPeter Ujfalusi 	struct twl6040_clk *twl6040_clk = container_of(hw, struct twl6040_clk,
41f9f8c043SPeter Ujfalusi 						       mcpdm_fclk);
42f9f8c043SPeter Ujfalusi 	return twl6040_clk->enabled;
43f9f8c043SPeter Ujfalusi }
44f9f8c043SPeter Ujfalusi 
45f9f8c043SPeter Ujfalusi static int twl6040_bitclk_prepare(struct clk_hw *hw)
46f9f8c043SPeter Ujfalusi {
47f9f8c043SPeter Ujfalusi 	struct twl6040_clk *twl6040_clk = container_of(hw, struct twl6040_clk,
48f9f8c043SPeter Ujfalusi 						       mcpdm_fclk);
49f9f8c043SPeter Ujfalusi 	int ret;
50f9f8c043SPeter Ujfalusi 
51f9f8c043SPeter Ujfalusi 	ret = twl6040_power(twl6040_clk->twl6040, 1);
52f9f8c043SPeter Ujfalusi 	if (!ret)
53f9f8c043SPeter Ujfalusi 		twl6040_clk->enabled = 1;
54f9f8c043SPeter Ujfalusi 
55f9f8c043SPeter Ujfalusi 	return ret;
56f9f8c043SPeter Ujfalusi }
57f9f8c043SPeter Ujfalusi 
58f9f8c043SPeter Ujfalusi static void twl6040_bitclk_unprepare(struct clk_hw *hw)
59f9f8c043SPeter Ujfalusi {
60f9f8c043SPeter Ujfalusi 	struct twl6040_clk *twl6040_clk = container_of(hw, struct twl6040_clk,
61f9f8c043SPeter Ujfalusi 						       mcpdm_fclk);
62f9f8c043SPeter Ujfalusi 	int ret;
63f9f8c043SPeter Ujfalusi 
64f9f8c043SPeter Ujfalusi 	ret = twl6040_power(twl6040_clk->twl6040, 0);
65f9f8c043SPeter Ujfalusi 	if (!ret)
66f9f8c043SPeter Ujfalusi 		twl6040_clk->enabled = 0;
67f9f8c043SPeter Ujfalusi }
68f9f8c043SPeter Ujfalusi 
69f9f8c043SPeter Ujfalusi static const struct clk_ops twl6040_mcpdm_ops = {
70f9f8c043SPeter Ujfalusi 	.is_enabled = twl6040_bitclk_is_enabled,
71f9f8c043SPeter Ujfalusi 	.prepare = twl6040_bitclk_prepare,
72f9f8c043SPeter Ujfalusi 	.unprepare = twl6040_bitclk_unprepare,
73f9f8c043SPeter Ujfalusi };
74f9f8c043SPeter Ujfalusi 
75f9f8c043SPeter Ujfalusi static struct clk_init_data wm831x_clkout_init = {
76f9f8c043SPeter Ujfalusi 	.name = "mcpdm_fclk",
77f9f8c043SPeter Ujfalusi 	.ops = &twl6040_mcpdm_ops,
78f9f8c043SPeter Ujfalusi 	.flags = CLK_IS_ROOT,
79f9f8c043SPeter Ujfalusi };
80f9f8c043SPeter Ujfalusi 
81f9f8c043SPeter Ujfalusi static int __devinit twl6040_clk_probe(struct platform_device *pdev)
82f9f8c043SPeter Ujfalusi {
83f9f8c043SPeter Ujfalusi 	struct twl6040 *twl6040 = dev_get_drvdata(pdev->dev.parent);
84f9f8c043SPeter Ujfalusi 	struct twl6040_clk *clkdata;
85f9f8c043SPeter Ujfalusi 
86f9f8c043SPeter Ujfalusi 	clkdata = devm_kzalloc(&pdev->dev, sizeof(*clkdata), GFP_KERNEL);
87f9f8c043SPeter Ujfalusi 	if (!clkdata)
88f9f8c043SPeter Ujfalusi 		return -ENOMEM;
89f9f8c043SPeter Ujfalusi 
90f9f8c043SPeter Ujfalusi 	clkdata->dev = &pdev->dev;
91f9f8c043SPeter Ujfalusi 	clkdata->twl6040 = twl6040;
92f9f8c043SPeter Ujfalusi 
93f9f8c043SPeter Ujfalusi 	clkdata->mcpdm_fclk.init = &wm831x_clkout_init;
94f9f8c043SPeter Ujfalusi 	clkdata->clk = clk_register(&pdev->dev, &clkdata->mcpdm_fclk);
95*ddc07ef9SWei Yongjun 	if (IS_ERR(clkdata->clk))
96*ddc07ef9SWei Yongjun 		return PTR_ERR(clkdata->clk);
97f9f8c043SPeter Ujfalusi 
98f9f8c043SPeter Ujfalusi 	dev_set_drvdata(&pdev->dev, clkdata);
99f9f8c043SPeter Ujfalusi 
100f9f8c043SPeter Ujfalusi 	return 0;
101f9f8c043SPeter Ujfalusi }
102f9f8c043SPeter Ujfalusi 
103f9f8c043SPeter Ujfalusi static int __devexit twl6040_clk_remove(struct platform_device *pdev)
104f9f8c043SPeter Ujfalusi {
105f9f8c043SPeter Ujfalusi 	struct twl6040_clk *clkdata = dev_get_drvdata(&pdev->dev);
106f9f8c043SPeter Ujfalusi 
107f9f8c043SPeter Ujfalusi 	clk_unregister(clkdata->clk);
108f9f8c043SPeter Ujfalusi 
109f9f8c043SPeter Ujfalusi 	return 0;
110f9f8c043SPeter Ujfalusi }
111f9f8c043SPeter Ujfalusi 
112f9f8c043SPeter Ujfalusi static struct platform_driver twl6040_clk_driver = {
113f9f8c043SPeter Ujfalusi 	.driver = {
114f9f8c043SPeter Ujfalusi 		.name = "twl6040-clk",
115f9f8c043SPeter Ujfalusi 		.owner = THIS_MODULE,
116f9f8c043SPeter Ujfalusi 	},
117f9f8c043SPeter Ujfalusi 	.probe = twl6040_clk_probe,
118f9f8c043SPeter Ujfalusi 	.remove = __devexit_p(twl6040_clk_remove),
119f9f8c043SPeter Ujfalusi };
120f9f8c043SPeter Ujfalusi 
121f9f8c043SPeter Ujfalusi module_platform_driver(twl6040_clk_driver);
122f9f8c043SPeter Ujfalusi 
123f9f8c043SPeter Ujfalusi MODULE_DESCRIPTION("TWL6040 clock driver for McPDM functional clock");
124f9f8c043SPeter Ujfalusi MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
125f9f8c043SPeter Ujfalusi MODULE_ALIAS("platform:twl6040-clk");
126f9f8c043SPeter Ujfalusi MODULE_LICENSE("GPL");
127