blob: 243072366aa82abc4c0f9804ced263c84efe28c4 [file] [log] [blame]
Jonghwa Lee73118e62012-08-28 17:54:28 +09001/*
2 * clk-max77686.c - Clock driver for Maxim 77686
3 *
4 * Copyright (C) 2012 Samsung Electornics
5 * Jonghwa Lee <jonghwa3.lee@samsung.com>
6 *
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.
11 *
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.
16 *
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
20 *
21 */
22
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>
32
33enum {
34 MAX77686_CLK_AP = 0,
35 MAX77686_CLK_CP,
36 MAX77686_CLK_PMIC,
37 MAX77686_CLKS_NUM,
38};
39
40struct max77686_clk {
41 struct max77686_dev *iodev;
42 u32 mask;
43 struct clk_hw hw;
44 struct clk_lookup *lookup;
45};
46
Axel Lin3fe296c2012-12-18 15:54:52 +080047static struct max77686_clk *to_max77686_clk(struct clk_hw *hw)
Jonghwa Lee73118e62012-08-28 17:54:28 +090048{
49 return container_of(hw, struct max77686_clk, hw);
50}
51
52static int max77686_clk_prepare(struct clk_hw *hw)
53{
Axel Lin3fe296c2012-12-18 15:54:52 +080054 struct max77686_clk *max77686 = to_max77686_clk(hw);
Jonghwa Lee73118e62012-08-28 17:54:28 +090055
Axel Lin3fe296c2012-12-18 15:54:52 +080056 return regmap_update_bits(max77686->iodev->regmap,
57 MAX77686_REG_32KHZ, max77686->mask,
58 max77686->mask);
Jonghwa Lee73118e62012-08-28 17:54:28 +090059}
60
61static void max77686_clk_unprepare(struct clk_hw *hw)
62{
Axel Lin3fe296c2012-12-18 15:54:52 +080063 struct max77686_clk *max77686 = to_max77686_clk(hw);
Jonghwa Lee73118e62012-08-28 17:54:28 +090064
65 regmap_update_bits(max77686->iodev->regmap,
66 MAX77686_REG_32KHZ, max77686->mask, ~max77686->mask);
67}
68
Tomasz Figa21c8ed22013-12-12 17:07:14 +010069static int max77686_clk_is_prepared(struct clk_hw *hw)
Jonghwa Lee73118e62012-08-28 17:54:28 +090070{
Axel Lin3fe296c2012-12-18 15:54:52 +080071 struct max77686_clk *max77686 = to_max77686_clk(hw);
Jonghwa Lee73118e62012-08-28 17:54:28 +090072 int ret;
73 u32 val;
74
Jonghwa Lee73118e62012-08-28 17:54:28 +090075 ret = regmap_read(max77686->iodev->regmap,
76 MAX77686_REG_32KHZ, &val);
77
78 if (ret < 0)
79 return -EINVAL;
80
81 return val & max77686->mask;
82}
83
Tomasz Figacf7d4a62013-12-12 17:07:15 +010084static unsigned long max77686_recalc_rate(struct clk_hw *hw,
85 unsigned long parent_rate)
86{
87 return 32768;
88}
89
Jonghwa Lee73118e62012-08-28 17:54:28 +090090static struct clk_ops max77686_clk_ops = {
91 .prepare = max77686_clk_prepare,
92 .unprepare = max77686_clk_unprepare,
Tomasz Figa21c8ed22013-12-12 17:07:14 +010093 .is_prepared = max77686_clk_is_prepared,
Tomasz Figacf7d4a62013-12-12 17:07:15 +010094 .recalc_rate = max77686_recalc_rate,
Jonghwa Lee73118e62012-08-28 17:54:28 +090095};
96
97static struct clk_init_data max77686_clks_init[MAX77686_CLKS_NUM] = {
98 [MAX77686_CLK_AP] = {
99 .name = "32khz_ap",
100 .ops = &max77686_clk_ops,
101 .flags = CLK_IS_ROOT,
102 },
103 [MAX77686_CLK_CP] = {
104 .name = "32khz_cp",
105 .ops = &max77686_clk_ops,
106 .flags = CLK_IS_ROOT,
107 },
108 [MAX77686_CLK_PMIC] = {
109 .name = "32khz_pmic",
110 .ops = &max77686_clk_ops,
111 .flags = CLK_IS_ROOT,
112 },
113};
114
Tomasz Figabadbc542013-12-12 17:07:17 +0100115static struct clk *max77686_clk_register(struct device *dev,
Jonghwa Lee73118e62012-08-28 17:54:28 +0900116 struct max77686_clk *max77686)
117{
118 struct clk *clk;
119 struct clk_hw *hw = &max77686->hw;
120
121 clk = clk_register(dev, hw);
Jonghwa Lee73118e62012-08-28 17:54:28 +0900122 if (IS_ERR(clk))
Tomasz Figabadbc542013-12-12 17:07:17 +0100123 return clk;
Jonghwa Lee73118e62012-08-28 17:54:28 +0900124
Laurent Pinchartf1ba28a2013-01-07 02:05:19 +0100125 max77686->lookup = kzalloc(sizeof(struct clk_lookup), GFP_KERNEL);
Axel Lin9f58b9b2012-12-18 15:43:45 +0800126 if (!max77686->lookup)
Tomasz Figabadbc542013-12-12 17:07:17 +0100127 return ERR_PTR(-ENOMEM);
Jonghwa Lee73118e62012-08-28 17:54:28 +0900128
129 max77686->lookup->con_id = hw->init->name;
130 max77686->lookup->clk = clk;
131
132 clkdev_add(max77686->lookup);
133
Tomasz Figabadbc542013-12-12 17:07:17 +0100134 return clk;
Jonghwa Lee73118e62012-08-28 17:54:28 +0900135}
136
Bill Pemberton018ae932012-11-19 13:22:52 -0500137static int max77686_clk_probe(struct platform_device *pdev)
Jonghwa Lee73118e62012-08-28 17:54:28 +0900138{
139 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
140 struct max77686_clk **max77686_clks;
141 int i, ret;
142
143 max77686_clks = devm_kzalloc(&pdev->dev, sizeof(struct max77686_clk *)
144 * MAX77686_CLKS_NUM, GFP_KERNEL);
Axel Lin9f58b9b2012-12-18 15:43:45 +0800145 if (!max77686_clks)
Jonghwa Lee73118e62012-08-28 17:54:28 +0900146 return -ENOMEM;
147
148 for (i = 0; i < MAX77686_CLKS_NUM; i++) {
149 max77686_clks[i] = devm_kzalloc(&pdev->dev,
150 sizeof(struct max77686_clk), GFP_KERNEL);
Axel Lin9f58b9b2012-12-18 15:43:45 +0800151 if (!max77686_clks[i])
Jonghwa Lee73118e62012-08-28 17:54:28 +0900152 return -ENOMEM;
153 }
154
155 for (i = 0; i < MAX77686_CLKS_NUM; i++) {
Tomasz Figabadbc542013-12-12 17:07:17 +0100156 struct clk *clk;
157
Jonghwa Lee73118e62012-08-28 17:54:28 +0900158 max77686_clks[i]->iodev = iodev;
159 max77686_clks[i]->mask = 1 << i;
160 max77686_clks[i]->hw.init = &max77686_clks_init[i];
161
Tomasz Figabadbc542013-12-12 17:07:17 +0100162 clk = max77686_clk_register(&pdev->dev, max77686_clks[i]);
163 if (IS_ERR(clk)) {
164 ret = PTR_ERR(clk);
165
Jonghwa Lee73118e62012-08-28 17:54:28 +0900166 switch (i) {
167 case MAX77686_CLK_AP:
168 dev_err(&pdev->dev, "Fail to register CLK_AP\n");
169 goto err_clk_ap;
Jonghwa Lee73118e62012-08-28 17:54:28 +0900170 case MAX77686_CLK_CP:
171 dev_err(&pdev->dev, "Fail to register CLK_CP\n");
172 goto err_clk_cp;
Jonghwa Lee73118e62012-08-28 17:54:28 +0900173 case MAX77686_CLK_PMIC:
174 dev_err(&pdev->dev, "Fail to register CLK_PMIC\n");
175 goto err_clk_pmic;
176 }
177 }
178 }
179
180 platform_set_drvdata(pdev, max77686_clks);
181
Tomasz Figab0f85172013-12-12 17:07:16 +0100182 return 0;
Jonghwa Lee73118e62012-08-28 17:54:28 +0900183
184err_clk_pmic:
185 clkdev_drop(max77686_clks[MAX77686_CLK_CP]->lookup);
186 kfree(max77686_clks[MAX77686_CLK_CP]->hw.clk);
187err_clk_cp:
188 clkdev_drop(max77686_clks[MAX77686_CLK_AP]->lookup);
189 kfree(max77686_clks[MAX77686_CLK_AP]->hw.clk);
190err_clk_ap:
Jonghwa Lee73118e62012-08-28 17:54:28 +0900191 return ret;
192}
193
Bill Pemberton1fc7ad52012-11-19 13:25:43 -0500194static int max77686_clk_remove(struct platform_device *pdev)
Jonghwa Lee73118e62012-08-28 17:54:28 +0900195{
196 struct max77686_clk **max77686_clks = platform_get_drvdata(pdev);
197 int i;
198
199 for (i = 0; i < MAX77686_CLKS_NUM; i++) {
200 clkdev_drop(max77686_clks[i]->lookup);
201 kfree(max77686_clks[i]->hw.clk);
202 }
203 return 0;
204}
205
206static const struct platform_device_id max77686_clk_id[] = {
207 { "max77686-clk", 0},
208 { },
209};
210MODULE_DEVICE_TABLE(platform, max77686_clk_id);
211
212static struct platform_driver max77686_clk_driver = {
213 .driver = {
214 .name = "max77686-clk",
215 .owner = THIS_MODULE,
216 },
217 .probe = max77686_clk_probe,
Bill Pembertonf9cfa632012-11-19 13:19:59 -0500218 .remove = max77686_clk_remove,
Jonghwa Lee73118e62012-08-28 17:54:28 +0900219 .id_table = max77686_clk_id,
220};
221
222static int __init max77686_clk_init(void)
223{
224 return platform_driver_register(&max77686_clk_driver);
225}
226subsys_initcall(max77686_clk_init);
227
228static void __init max77686_clk_cleanup(void)
229{
230 platform_driver_unregister(&max77686_clk_driver);
231}
232module_exit(max77686_clk_cleanup);
233
234MODULE_DESCRIPTION("MAXIM 77686 Clock Driver");
235MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
236MODULE_LICENSE("GPL");