2012-08-28 08:54:28 +00:00
|
|
|
/*
|
|
|
|
* clk-max77686.c - Clock driver for Maxim 77686
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Samsung Electornics
|
|
|
|
* Jonghwa Lee <jonghwa3.lee@samsung.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/mfd/max77686.h>
|
|
|
|
#include <linux/mfd/max77686-private.h>
|
|
|
|
#include <linux/clk-provider.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/clkdev.h>
|
|
|
|
|
|
|
|
enum {
|
|
|
|
MAX77686_CLK_AP = 0,
|
|
|
|
MAX77686_CLK_CP,
|
|
|
|
MAX77686_CLK_PMIC,
|
|
|
|
MAX77686_CLKS_NUM,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct max77686_clk {
|
|
|
|
struct max77686_dev *iodev;
|
|
|
|
u32 mask;
|
|
|
|
struct clk_hw hw;
|
|
|
|
struct clk_lookup *lookup;
|
|
|
|
};
|
|
|
|
|
2012-12-18 07:54:52 +00:00
|
|
|
static struct max77686_clk *to_max77686_clk(struct clk_hw *hw)
|
2012-08-28 08:54:28 +00:00
|
|
|
{
|
|
|
|
return container_of(hw, struct max77686_clk, hw);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int max77686_clk_prepare(struct clk_hw *hw)
|
|
|
|
{
|
2012-12-18 07:54:52 +00:00
|
|
|
struct max77686_clk *max77686 = to_max77686_clk(hw);
|
2012-08-28 08:54:28 +00:00
|
|
|
|
2012-12-18 07:54:52 +00:00
|
|
|
return regmap_update_bits(max77686->iodev->regmap,
|
|
|
|
MAX77686_REG_32KHZ, max77686->mask,
|
|
|
|
max77686->mask);
|
2012-08-28 08:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void max77686_clk_unprepare(struct clk_hw *hw)
|
|
|
|
{
|
2012-12-18 07:54:52 +00:00
|
|
|
struct max77686_clk *max77686 = to_max77686_clk(hw);
|
2012-08-28 08:54:28 +00:00
|
|
|
|
|
|
|
regmap_update_bits(max77686->iodev->regmap,
|
|
|
|
MAX77686_REG_32KHZ, max77686->mask, ~max77686->mask);
|
|
|
|
}
|
|
|
|
|
2013-12-12 16:07:14 +00:00
|
|
|
static int max77686_clk_is_prepared(struct clk_hw *hw)
|
2012-08-28 08:54:28 +00:00
|
|
|
{
|
2012-12-18 07:54:52 +00:00
|
|
|
struct max77686_clk *max77686 = to_max77686_clk(hw);
|
2012-08-28 08:54:28 +00:00
|
|
|
int ret;
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
ret = regmap_read(max77686->iodev->regmap,
|
|
|
|
MAX77686_REG_32KHZ, &val);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return val & max77686->mask;
|
|
|
|
}
|
|
|
|
|
2013-12-12 16:07:15 +00:00
|
|
|
static unsigned long max77686_recalc_rate(struct clk_hw *hw,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
return 32768;
|
|
|
|
}
|
|
|
|
|
2012-08-28 08:54:28 +00:00
|
|
|
static struct clk_ops max77686_clk_ops = {
|
|
|
|
.prepare = max77686_clk_prepare,
|
|
|
|
.unprepare = max77686_clk_unprepare,
|
2013-12-12 16:07:14 +00:00
|
|
|
.is_prepared = max77686_clk_is_prepared,
|
2013-12-12 16:07:15 +00:00
|
|
|
.recalc_rate = max77686_recalc_rate,
|
2012-08-28 08:54:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct clk_init_data max77686_clks_init[MAX77686_CLKS_NUM] = {
|
|
|
|
[MAX77686_CLK_AP] = {
|
|
|
|
.name = "32khz_ap",
|
|
|
|
.ops = &max77686_clk_ops,
|
|
|
|
.flags = CLK_IS_ROOT,
|
|
|
|
},
|
|
|
|
[MAX77686_CLK_CP] = {
|
|
|
|
.name = "32khz_cp",
|
|
|
|
.ops = &max77686_clk_ops,
|
|
|
|
.flags = CLK_IS_ROOT,
|
|
|
|
},
|
|
|
|
[MAX77686_CLK_PMIC] = {
|
|
|
|
.name = "32khz_pmic",
|
|
|
|
.ops = &max77686_clk_ops,
|
|
|
|
.flags = CLK_IS_ROOT,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2013-12-12 16:07:17 +00:00
|
|
|
static struct clk *max77686_clk_register(struct device *dev,
|
2012-08-28 08:54:28 +00:00
|
|
|
struct max77686_clk *max77686)
|
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
struct clk_hw *hw = &max77686->hw;
|
|
|
|
|
|
|
|
clk = clk_register(dev, hw);
|
|
|
|
if (IS_ERR(clk))
|
2013-12-12 16:07:17 +00:00
|
|
|
return clk;
|
2012-08-28 08:54:28 +00:00
|
|
|
|
2013-01-07 01:05:19 +00:00
|
|
|
max77686->lookup = kzalloc(sizeof(struct clk_lookup), GFP_KERNEL);
|
2012-12-18 07:43:45 +00:00
|
|
|
if (!max77686->lookup)
|
2013-12-12 16:07:17 +00:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2012-08-28 08:54:28 +00:00
|
|
|
|
|
|
|
max77686->lookup->con_id = hw->init->name;
|
|
|
|
max77686->lookup->clk = clk;
|
|
|
|
|
|
|
|
clkdev_add(max77686->lookup);
|
|
|
|
|
2013-12-12 16:07:17 +00:00
|
|
|
return clk;
|
2012-08-28 08:54:28 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 18:22:52 +00:00
|
|
|
static int max77686_clk_probe(struct platform_device *pdev)
|
2012-08-28 08:54:28 +00:00
|
|
|
{
|
|
|
|
struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
|
|
|
|
struct max77686_clk **max77686_clks;
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
max77686_clks = devm_kzalloc(&pdev->dev, sizeof(struct max77686_clk *)
|
|
|
|
* MAX77686_CLKS_NUM, GFP_KERNEL);
|
2012-12-18 07:43:45 +00:00
|
|
|
if (!max77686_clks)
|
2012-08-28 08:54:28 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX77686_CLKS_NUM; i++) {
|
|
|
|
max77686_clks[i] = devm_kzalloc(&pdev->dev,
|
|
|
|
sizeof(struct max77686_clk), GFP_KERNEL);
|
2012-12-18 07:43:45 +00:00
|
|
|
if (!max77686_clks[i])
|
2012-08-28 08:54:28 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < MAX77686_CLKS_NUM; i++) {
|
2013-12-12 16:07:17 +00:00
|
|
|
struct clk *clk;
|
|
|
|
|
2012-08-28 08:54:28 +00:00
|
|
|
max77686_clks[i]->iodev = iodev;
|
|
|
|
max77686_clks[i]->mask = 1 << i;
|
|
|
|
max77686_clks[i]->hw.init = &max77686_clks_init[i];
|
|
|
|
|
2013-12-12 16:07:17 +00:00
|
|
|
clk = max77686_clk_register(&pdev->dev, max77686_clks[i]);
|
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
ret = PTR_ERR(clk);
|
|
|
|
|
2013-12-12 16:07:18 +00:00
|
|
|
dev_err(&pdev->dev, "failed to register %s\n",
|
|
|
|
max77686_clks[i]->hw.init->name);
|
|
|
|
goto err_clocks;
|
2012-08-28 08:54:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, max77686_clks);
|
|
|
|
|
2013-12-12 16:07:16 +00:00
|
|
|
return 0;
|
2012-08-28 08:54:28 +00:00
|
|
|
|
2013-12-12 16:07:18 +00:00
|
|
|
err_clocks:
|
|
|
|
for (--i; i >= 0; --i) {
|
|
|
|
clkdev_drop(max77686_clks[i]->lookup);
|
|
|
|
clk_unregister(max77686_clks[i]->hw.clk);
|
|
|
|
}
|
|
|
|
|
2012-08-28 08:54:28 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-19 18:25:43 +00:00
|
|
|
static int max77686_clk_remove(struct platform_device *pdev)
|
2012-08-28 08:54:28 +00:00
|
|
|
{
|
|
|
|
struct max77686_clk **max77686_clks = platform_get_drvdata(pdev);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX77686_CLKS_NUM; i++) {
|
|
|
|
clkdev_drop(max77686_clks[i]->lookup);
|
2013-12-12 16:07:18 +00:00
|
|
|
clk_unregister(max77686_clks[i]->hw.clk);
|
2012-08-28 08:54:28 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct platform_device_id max77686_clk_id[] = {
|
|
|
|
{ "max77686-clk", 0},
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(platform, max77686_clk_id);
|
|
|
|
|
|
|
|
static struct platform_driver max77686_clk_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "max77686-clk",
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
},
|
|
|
|
.probe = max77686_clk_probe,
|
2012-11-19 18:19:59 +00:00
|
|
|
.remove = max77686_clk_remove,
|
2012-08-28 08:54:28 +00:00
|
|
|
.id_table = max77686_clk_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init max77686_clk_init(void)
|
|
|
|
{
|
|
|
|
return platform_driver_register(&max77686_clk_driver);
|
|
|
|
}
|
|
|
|
subsys_initcall(max77686_clk_init);
|
|
|
|
|
|
|
|
static void __init max77686_clk_cleanup(void)
|
|
|
|
{
|
|
|
|
platform_driver_unregister(&max77686_clk_driver);
|
|
|
|
}
|
|
|
|
module_exit(max77686_clk_cleanup);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("MAXIM 77686 Clock Driver");
|
|
|
|
MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
|
|
|
|
MODULE_LICENSE("GPL");
|