2 * clk-max77686.c - Clock driver for Maxim 77686
4 * Copyright (C) 2012 Samsung Electornics
5 * Jonghwa Lee <jonghwa3.lee@samsung.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/err.h>
26 #include <linux/platform_device.h>
27 #include <linux/mfd/max77686.h>
28 #include <linux/mfd/max77686-private.h>
29 #include <linux/clk-provider.h>
30 #include <linux/mutex.h>
31 #include <linux/clkdev.h>
41 struct max77686_dev *iodev;
44 struct clk_lookup *lookup;
47 static struct max77686_clk *to_max77686_clk(struct clk_hw *hw)
49 return container_of(hw, struct max77686_clk, hw);
52 static int max77686_clk_prepare(struct clk_hw *hw)
54 struct max77686_clk *max77686 = to_max77686_clk(hw);
56 return regmap_update_bits(max77686->iodev->regmap,
57 MAX77686_REG_32KHZ, max77686->mask,
61 static void max77686_clk_unprepare(struct clk_hw *hw)
63 struct max77686_clk *max77686 = to_max77686_clk(hw);
65 regmap_update_bits(max77686->iodev->regmap,
66 MAX77686_REG_32KHZ, max77686->mask, ~max77686->mask);
69 static int max77686_clk_is_prepared(struct clk_hw *hw)
71 struct max77686_clk *max77686 = to_max77686_clk(hw);
75 ret = regmap_read(max77686->iodev->regmap,
76 MAX77686_REG_32KHZ, &val);
81 return val & max77686->mask;
84 static unsigned long max77686_recalc_rate(struct clk_hw *hw,
85 unsigned long parent_rate)
90 static struct clk_ops max77686_clk_ops = {
91 .prepare = max77686_clk_prepare,
92 .unprepare = max77686_clk_unprepare,
93 .is_prepared = max77686_clk_is_prepared,
94 .recalc_rate = max77686_recalc_rate,
97 static struct clk_init_data max77686_clks_init[MAX77686_CLKS_NUM] = {
100 .ops = &max77686_clk_ops,
101 .flags = CLK_IS_ROOT,
103 [MAX77686_CLK_CP] = {
105 .ops = &max77686_clk_ops,
106 .flags = CLK_IS_ROOT,
108 [MAX77686_CLK_PMIC] = {
109 .name = "32khz_pmic",
110 .ops = &max77686_clk_ops,
111 .flags = CLK_IS_ROOT,
115 static int max77686_clk_register(struct device *dev,
116 struct max77686_clk *max77686)
119 struct clk_hw *hw = &max77686->hw;
121 clk = clk_register(dev, hw);
126 max77686->lookup = kzalloc(sizeof(struct clk_lookup), GFP_KERNEL);
127 if (!max77686->lookup)
130 max77686->lookup->con_id = hw->init->name;
131 max77686->lookup->clk = clk;
133 clkdev_add(max77686->lookup);
138 static int max77686_clk_probe(struct platform_device *pdev)
140 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
141 struct max77686_clk **max77686_clks;
144 max77686_clks = devm_kzalloc(&pdev->dev, sizeof(struct max77686_clk *)
145 * MAX77686_CLKS_NUM, GFP_KERNEL);
149 for (i = 0; i < MAX77686_CLKS_NUM; i++) {
150 max77686_clks[i] = devm_kzalloc(&pdev->dev,
151 sizeof(struct max77686_clk), GFP_KERNEL);
152 if (!max77686_clks[i])
156 for (i = 0; i < MAX77686_CLKS_NUM; i++) {
157 max77686_clks[i]->iodev = iodev;
158 max77686_clks[i]->mask = 1 << i;
159 max77686_clks[i]->hw.init = &max77686_clks_init[i];
161 ret = max77686_clk_register(&pdev->dev, max77686_clks[i]);
164 case MAX77686_CLK_AP:
165 dev_err(&pdev->dev, "Fail to register CLK_AP\n");
167 case MAX77686_CLK_CP:
168 dev_err(&pdev->dev, "Fail to register CLK_CP\n");
170 case MAX77686_CLK_PMIC:
171 dev_err(&pdev->dev, "Fail to register CLK_PMIC\n");
177 platform_set_drvdata(pdev, max77686_clks);
182 clkdev_drop(max77686_clks[MAX77686_CLK_CP]->lookup);
183 kfree(max77686_clks[MAX77686_CLK_CP]->hw.clk);
185 clkdev_drop(max77686_clks[MAX77686_CLK_AP]->lookup);
186 kfree(max77686_clks[MAX77686_CLK_AP]->hw.clk);
191 static int max77686_clk_remove(struct platform_device *pdev)
193 struct max77686_clk **max77686_clks = platform_get_drvdata(pdev);
196 for (i = 0; i < MAX77686_CLKS_NUM; i++) {
197 clkdev_drop(max77686_clks[i]->lookup);
198 kfree(max77686_clks[i]->hw.clk);
203 static const struct platform_device_id max77686_clk_id[] = {
204 { "max77686-clk", 0},
207 MODULE_DEVICE_TABLE(platform, max77686_clk_id);
209 static struct platform_driver max77686_clk_driver = {
211 .name = "max77686-clk",
212 .owner = THIS_MODULE,
214 .probe = max77686_clk_probe,
215 .remove = max77686_clk_remove,
216 .id_table = max77686_clk_id,
219 static int __init max77686_clk_init(void)
221 return platform_driver_register(&max77686_clk_driver);
223 subsys_initcall(max77686_clk_init);
225 static void __init max77686_clk_cleanup(void)
227 platform_driver_unregister(&max77686_clk_driver);
229 module_exit(max77686_clk_cleanup);
231 MODULE_DESCRIPTION("MAXIM 77686 Clock Driver");
232 MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
233 MODULE_LICENSE("GPL");