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 447e37deb7SPeter Ujfalusi static int twl6040_pdmclk_prepare(struct clk_hw *hw) 45f9f8c043SPeter Ujfalusi { 467e37deb7SPeter Ujfalusi struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk, 477e37deb7SPeter Ujfalusi pdmclk_hw); 48f9f8c043SPeter Ujfalusi int ret; 49f9f8c043SPeter Ujfalusi 507e37deb7SPeter Ujfalusi ret = twl6040_power(pdmclk->twl6040, 1); 51f9f8c043SPeter Ujfalusi if (!ret) 527e37deb7SPeter Ujfalusi pdmclk->enabled = 1; 53f9f8c043SPeter Ujfalusi 54f9f8c043SPeter Ujfalusi return ret; 55f9f8c043SPeter Ujfalusi } 56f9f8c043SPeter Ujfalusi 577e37deb7SPeter Ujfalusi static void twl6040_pdmclk_unprepare(struct clk_hw *hw) 58f9f8c043SPeter Ujfalusi { 597e37deb7SPeter Ujfalusi struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk, 607e37deb7SPeter Ujfalusi pdmclk_hw); 61f9f8c043SPeter Ujfalusi int ret; 62f9f8c043SPeter Ujfalusi 637e37deb7SPeter Ujfalusi ret = twl6040_power(pdmclk->twl6040, 0); 64f9f8c043SPeter Ujfalusi if (!ret) 657e37deb7SPeter Ujfalusi pdmclk->enabled = 0; 667e37deb7SPeter Ujfalusi 67f9f8c043SPeter Ujfalusi } 68f9f8c043SPeter Ujfalusi 697e37deb7SPeter Ujfalusi static unsigned long twl6040_pdmclk_recalc_rate(struct clk_hw *hw, 707e37deb7SPeter Ujfalusi unsigned long parent_rate) 717e37deb7SPeter Ujfalusi { 727e37deb7SPeter Ujfalusi struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk, 737e37deb7SPeter Ujfalusi pdmclk_hw); 747e37deb7SPeter Ujfalusi 757e37deb7SPeter Ujfalusi return twl6040_get_sysclk(pdmclk->twl6040); 767e37deb7SPeter Ujfalusi } 777e37deb7SPeter Ujfalusi 787e37deb7SPeter Ujfalusi static const struct clk_ops twl6040_pdmclk_ops = { 797e37deb7SPeter Ujfalusi .is_prepared = twl6040_pdmclk_is_prepared, 807e37deb7SPeter Ujfalusi .prepare = twl6040_pdmclk_prepare, 817e37deb7SPeter Ujfalusi .unprepare = twl6040_pdmclk_unprepare, 827e37deb7SPeter Ujfalusi .recalc_rate = twl6040_pdmclk_recalc_rate, 83f9f8c043SPeter Ujfalusi }; 84f9f8c043SPeter Ujfalusi 850777066dSBhumika Goyal static const struct clk_init_data twl6040_pdmclk_init = { 867e37deb7SPeter Ujfalusi .name = "pdmclk", 877e37deb7SPeter Ujfalusi .ops = &twl6040_pdmclk_ops, 887e37deb7SPeter Ujfalusi .flags = CLK_GET_RATE_NOCACHE, 89f9f8c043SPeter Ujfalusi }; 90f9f8c043SPeter Ujfalusi 917e37deb7SPeter Ujfalusi static int twl6040_pdmclk_probe(struct platform_device *pdev) 92f9f8c043SPeter Ujfalusi { 93f9f8c043SPeter Ujfalusi struct twl6040 *twl6040 = dev_get_drvdata(pdev->dev.parent); 947e37deb7SPeter Ujfalusi struct twl6040_pdmclk *clkdata; 95f5b3715eSStephen Boyd int ret; 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 1047e37deb7SPeter Ujfalusi clkdata->pdmclk_hw.init = &twl6040_pdmclk_init; 105f5b3715eSStephen Boyd ret = devm_clk_hw_register(&pdev->dev, &clkdata->pdmclk_hw); 106f5b3715eSStephen Boyd if (ret) 107f5b3715eSStephen Boyd return ret; 108f9f8c043SPeter Ujfalusi 109c0431037SJingoo Han platform_set_drvdata(pdev, clkdata); 110f9f8c043SPeter Ujfalusi 111*654dea6eSMatti Vaittinen return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_simple_get, 112f5b3715eSStephen Boyd &clkdata->pdmclk_hw); 113f9f8c043SPeter Ujfalusi } 114f9f8c043SPeter Ujfalusi 1157e37deb7SPeter Ujfalusi static struct platform_driver twl6040_pdmclk_driver = { 116f9f8c043SPeter Ujfalusi .driver = { 1177e37deb7SPeter Ujfalusi .name = "twl6040-pdmclk", 118f9f8c043SPeter Ujfalusi }, 1197e37deb7SPeter Ujfalusi .probe = twl6040_pdmclk_probe, 120f9f8c043SPeter Ujfalusi }; 121f9f8c043SPeter Ujfalusi 1227e37deb7SPeter 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>"); 1267e37deb7SPeter Ujfalusi MODULE_ALIAS("platform:twl6040-pdmclk"); 127f9f8c043SPeter Ujfalusi MODULE_LICENSE("GPL"); 128