blob: 7b222a5db9319b46efe373e6fb859b412457a510 [file] [log] [blame]
Peter Ujfalusif9f8c042012-09-14 17:30:27 +03001/*
2* TWL6040 clock module driver for OMAP4 McPDM functional clock
3*
4* Copyright (C) 2012 Texas Instruments Inc.
5* Peter Ujfalusi <peter.ujfalusi@ti.com>
6*
7* This program is free software; you can redistribute it and/or
8* modify it under the terms of the GNU General Public License
9* version 2 as published by the Free Software Foundation.
10*
11* This program is distributed in the hope that it will be useful, but
12* WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14* General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License
17* along with this program; if not, write to the Free Software
18* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19* 02110-1301 USA
20*
21*/
22
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030023#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/platform_device.h>
26#include <linux/mfd/twl6040.h>
27#include <linux/clk-provider.h>
28
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030029struct twl6040_pdmclk {
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030030 struct twl6040 *twl6040;
31 struct device *dev;
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030032 struct clk_hw pdmclk_hw;
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030033 int enabled;
34};
35
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030036static int twl6040_pdmclk_is_prepared(struct clk_hw *hw)
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030037{
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030038 struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
39 pdmclk_hw);
40
41 return pdmclk->enabled;
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030042}
43
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030044static int twl6040_pdmclk_prepare(struct clk_hw *hw)
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030045{
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030046 struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
47 pdmclk_hw);
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030048 int ret;
49
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030050 ret = twl6040_power(pdmclk->twl6040, 1);
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030051 if (!ret)
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030052 pdmclk->enabled = 1;
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030053
54 return ret;
55}
56
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030057static void twl6040_pdmclk_unprepare(struct clk_hw *hw)
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030058{
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030059 struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
60 pdmclk_hw);
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030061 int ret;
62
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030063 ret = twl6040_power(pdmclk->twl6040, 0);
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030064 if (!ret)
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030065 pdmclk->enabled = 0;
66
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030067}
68
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030069static unsigned long twl6040_pdmclk_recalc_rate(struct clk_hw *hw,
70 unsigned long parent_rate)
71{
72 struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
73 pdmclk_hw);
74
75 return twl6040_get_sysclk(pdmclk->twl6040);
76}
77
78static const struct clk_ops twl6040_pdmclk_ops = {
79 .is_prepared = twl6040_pdmclk_is_prepared,
80 .prepare = twl6040_pdmclk_prepare,
81 .unprepare = twl6040_pdmclk_unprepare,
82 .recalc_rate = twl6040_pdmclk_recalc_rate,
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030083};
84
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030085static struct clk_init_data twl6040_pdmclk_init = {
86 .name = "pdmclk",
87 .ops = &twl6040_pdmclk_ops,
88 .flags = CLK_GET_RATE_NOCACHE,
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030089};
90
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030091static int twl6040_pdmclk_probe(struct platform_device *pdev)
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030092{
93 struct twl6040 *twl6040 = dev_get_drvdata(pdev->dev.parent);
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +030094 struct twl6040_pdmclk *clkdata;
Stephen Boydf5b37152016-06-01 16:15:30 -070095 int ret;
Peter Ujfalusif9f8c042012-09-14 17:30:27 +030096
97 clkdata = devm_kzalloc(&pdev->dev, sizeof(*clkdata), GFP_KERNEL);
98 if (!clkdata)
99 return -ENOMEM;
100
101 clkdata->dev = &pdev->dev;
102 clkdata->twl6040 = twl6040;
103
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +0300104 clkdata->pdmclk_hw.init = &twl6040_pdmclk_init;
Stephen Boydf5b37152016-06-01 16:15:30 -0700105 ret = devm_clk_hw_register(&pdev->dev, &clkdata->pdmclk_hw);
106 if (ret)
107 return ret;
Peter Ujfalusif9f8c042012-09-14 17:30:27 +0300108
Jingoo Hanc0431032013-05-24 10:11:46 +0900109 platform_set_drvdata(pdev, clkdata);
Peter Ujfalusif9f8c042012-09-14 17:30:27 +0300110
Stephen Boydf5b37152016-06-01 16:15:30 -0700111 return of_clk_add_hw_provider(pdev->dev.parent->of_node,
112 of_clk_hw_simple_get,
113 &clkdata->pdmclk_hw);
Peter Ujfalusif9f8c042012-09-14 17:30:27 +0300114}
115
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +0300116static struct platform_driver twl6040_pdmclk_driver = {
Peter Ujfalusif9f8c042012-09-14 17:30:27 +0300117 .driver = {
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +0300118 .name = "twl6040-pdmclk",
Peter Ujfalusif9f8c042012-09-14 17:30:27 +0300119 },
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +0300120 .probe = twl6040_pdmclk_probe,
Peter Ujfalusif9f8c042012-09-14 17:30:27 +0300121};
122
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +0300123module_platform_driver(twl6040_pdmclk_driver);
Peter Ujfalusif9f8c042012-09-14 17:30:27 +0300124
125MODULE_DESCRIPTION("TWL6040 clock driver for McPDM functional clock");
126MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
Peter Ujfalusi7e37deb2016-05-30 11:55:11 +0300127MODULE_ALIAS("platform:twl6040-pdmclk");
Peter Ujfalusif9f8c042012-09-14 17:30:27 +0300128MODULE_LICENSE("GPL");