blob: 185b611c957e6df0a2b54177e0ead748717a7ea3 [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
Javier Martinez Canillasa8a76f52014-08-18 10:32:59 +020033#include <dt-bindings/clock/maxim,max77686.h>
Jonghwa Lee73118e62012-08-28 17:54:28 +090034
35struct max77686_clk {
36 struct max77686_dev *iodev;
37 u32 mask;
38 struct clk_hw hw;
39 struct clk_lookup *lookup;
40};
41
Axel Lin3fe296c2012-12-18 15:54:52 +080042static struct max77686_clk *to_max77686_clk(struct clk_hw *hw)
Jonghwa Lee73118e62012-08-28 17:54:28 +090043{
44 return container_of(hw, struct max77686_clk, hw);
45}
46
47static int max77686_clk_prepare(struct clk_hw *hw)
48{
Axel Lin3fe296c2012-12-18 15:54:52 +080049 struct max77686_clk *max77686 = to_max77686_clk(hw);
Jonghwa Lee73118e62012-08-28 17:54:28 +090050
Axel Lin3fe296c2012-12-18 15:54:52 +080051 return regmap_update_bits(max77686->iodev->regmap,
52 MAX77686_REG_32KHZ, max77686->mask,
53 max77686->mask);
Jonghwa Lee73118e62012-08-28 17:54:28 +090054}
55
56static void max77686_clk_unprepare(struct clk_hw *hw)
57{
Axel Lin3fe296c2012-12-18 15:54:52 +080058 struct max77686_clk *max77686 = to_max77686_clk(hw);
Jonghwa Lee73118e62012-08-28 17:54:28 +090059
60 regmap_update_bits(max77686->iodev->regmap,
61 MAX77686_REG_32KHZ, max77686->mask, ~max77686->mask);
62}
63
Tomasz Figa21c8ed22013-12-12 17:07:14 +010064static int max77686_clk_is_prepared(struct clk_hw *hw)
Jonghwa Lee73118e62012-08-28 17:54:28 +090065{
Axel Lin3fe296c2012-12-18 15:54:52 +080066 struct max77686_clk *max77686 = to_max77686_clk(hw);
Jonghwa Lee73118e62012-08-28 17:54:28 +090067 int ret;
68 u32 val;
69
Jonghwa Lee73118e62012-08-28 17:54:28 +090070 ret = regmap_read(max77686->iodev->regmap,
71 MAX77686_REG_32KHZ, &val);
72
73 if (ret < 0)
74 return -EINVAL;
75
76 return val & max77686->mask;
77}
78
Tomasz Figacf7d4a62013-12-12 17:07:15 +010079static unsigned long max77686_recalc_rate(struct clk_hw *hw,
80 unsigned long parent_rate)
81{
82 return 32768;
83}
84
Jonghwa Lee73118e62012-08-28 17:54:28 +090085static struct clk_ops max77686_clk_ops = {
86 .prepare = max77686_clk_prepare,
87 .unprepare = max77686_clk_unprepare,
Tomasz Figa21c8ed22013-12-12 17:07:14 +010088 .is_prepared = max77686_clk_is_prepared,
Tomasz Figacf7d4a62013-12-12 17:07:15 +010089 .recalc_rate = max77686_recalc_rate,
Jonghwa Lee73118e62012-08-28 17:54:28 +090090};
91
92static struct clk_init_data max77686_clks_init[MAX77686_CLKS_NUM] = {
93 [MAX77686_CLK_AP] = {
94 .name = "32khz_ap",
95 .ops = &max77686_clk_ops,
96 .flags = CLK_IS_ROOT,
97 },
98 [MAX77686_CLK_CP] = {
99 .name = "32khz_cp",
100 .ops = &max77686_clk_ops,
101 .flags = CLK_IS_ROOT,
102 },
103 [MAX77686_CLK_PMIC] = {
104 .name = "32khz_pmic",
105 .ops = &max77686_clk_ops,
106 .flags = CLK_IS_ROOT,
107 },
108};
109
Tomasz Figabadbc542013-12-12 17:07:17 +0100110static struct clk *max77686_clk_register(struct device *dev,
Jonghwa Lee73118e62012-08-28 17:54:28 +0900111 struct max77686_clk *max77686)
112{
113 struct clk *clk;
114 struct clk_hw *hw = &max77686->hw;
115
116 clk = clk_register(dev, hw);
Jonghwa Lee73118e62012-08-28 17:54:28 +0900117 if (IS_ERR(clk))
Tomasz Figabadbc542013-12-12 17:07:17 +0100118 return clk;
Jonghwa Lee73118e62012-08-28 17:54:28 +0900119
Laurent Pinchartf1ba28a2013-01-07 02:05:19 +0100120 max77686->lookup = kzalloc(sizeof(struct clk_lookup), GFP_KERNEL);
Axel Lin9f58b9b2012-12-18 15:43:45 +0800121 if (!max77686->lookup)
Tomasz Figabadbc542013-12-12 17:07:17 +0100122 return ERR_PTR(-ENOMEM);
Jonghwa Lee73118e62012-08-28 17:54:28 +0900123
124 max77686->lookup->con_id = hw->init->name;
125 max77686->lookup->clk = clk;
126
127 clkdev_add(max77686->lookup);
128
Tomasz Figabadbc542013-12-12 17:07:17 +0100129 return clk;
Jonghwa Lee73118e62012-08-28 17:54:28 +0900130}
131
Bill Pemberton018ae932012-11-19 13:22:52 -0500132static int max77686_clk_probe(struct platform_device *pdev)
Jonghwa Lee73118e62012-08-28 17:54:28 +0900133{
134 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Tomasz Figa3966c942013-12-12 17:07:19 +0100135 struct max77686_clk *max77686_clks[MAX77686_CLKS_NUM];
136 struct clk **clocks;
Jonghwa Lee73118e62012-08-28 17:54:28 +0900137 int i, ret;
138
Tomasz Figa3966c942013-12-12 17:07:19 +0100139 clocks = devm_kzalloc(&pdev->dev, sizeof(struct clk *)
Jonghwa Lee73118e62012-08-28 17:54:28 +0900140 * MAX77686_CLKS_NUM, GFP_KERNEL);
Tomasz Figa3966c942013-12-12 17:07:19 +0100141 if (!clocks)
Jonghwa Lee73118e62012-08-28 17:54:28 +0900142 return -ENOMEM;
143
144 for (i = 0; i < MAX77686_CLKS_NUM; i++) {
145 max77686_clks[i] = devm_kzalloc(&pdev->dev,
146 sizeof(struct max77686_clk), GFP_KERNEL);
Axel Lin9f58b9b2012-12-18 15:43:45 +0800147 if (!max77686_clks[i])
Jonghwa Lee73118e62012-08-28 17:54:28 +0900148 return -ENOMEM;
149 }
150
151 for (i = 0; i < MAX77686_CLKS_NUM; i++) {
152 max77686_clks[i]->iodev = iodev;
153 max77686_clks[i]->mask = 1 << i;
154 max77686_clks[i]->hw.init = &max77686_clks_init[i];
155
Tomasz Figa3966c942013-12-12 17:07:19 +0100156 clocks[i] = max77686_clk_register(&pdev->dev, max77686_clks[i]);
157 if (IS_ERR(clocks[i])) {
158 ret = PTR_ERR(clocks[i]);
Tomasz Figad73ac4c2013-12-12 17:07:18 +0100159 dev_err(&pdev->dev, "failed to register %s\n",
160 max77686_clks[i]->hw.init->name);
161 goto err_clocks;
Jonghwa Lee73118e62012-08-28 17:54:28 +0900162 }
163 }
164
Tomasz Figa3966c942013-12-12 17:07:19 +0100165 platform_set_drvdata(pdev, clocks);
Jonghwa Lee73118e62012-08-28 17:54:28 +0900166
Tomasz Figab06c6982013-12-12 17:07:20 +0100167 if (iodev->dev->of_node) {
168 struct clk_onecell_data *of_data;
169
170 of_data = devm_kzalloc(&pdev->dev,
171 sizeof(*of_data), GFP_KERNEL);
172 if (!of_data) {
173 ret = -ENOMEM;
174 goto err_clocks;
175 }
176
177 of_data->clks = clocks;
178 of_data->clk_num = MAX77686_CLKS_NUM;
179 ret = of_clk_add_provider(iodev->dev->of_node,
180 of_clk_src_onecell_get, of_data);
181 if (ret) {
182 dev_err(&pdev->dev, "failed to register OF clock provider\n");
183 goto err_clocks;
184 }
185 }
186
Tomasz Figab0f85172013-12-12 17:07:16 +0100187 return 0;
Jonghwa Lee73118e62012-08-28 17:54:28 +0900188
Tomasz Figad73ac4c2013-12-12 17:07:18 +0100189err_clocks:
190 for (--i; i >= 0; --i) {
191 clkdev_drop(max77686_clks[i]->lookup);
192 clk_unregister(max77686_clks[i]->hw.clk);
193 }
194
Jonghwa Lee73118e62012-08-28 17:54:28 +0900195 return ret;
196}
197
Bill Pemberton1fc7ad52012-11-19 13:25:43 -0500198static int max77686_clk_remove(struct platform_device *pdev)
Jonghwa Lee73118e62012-08-28 17:54:28 +0900199{
Tomasz Figab06c6982013-12-12 17:07:20 +0100200 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Tomasz Figa3966c942013-12-12 17:07:19 +0100201 struct clk **clocks = platform_get_drvdata(pdev);
Jonghwa Lee73118e62012-08-28 17:54:28 +0900202 int i;
203
Tomasz Figab06c6982013-12-12 17:07:20 +0100204 if (iodev->dev->of_node)
205 of_clk_del_provider(iodev->dev->of_node);
206
Jonghwa Lee73118e62012-08-28 17:54:28 +0900207 for (i = 0; i < MAX77686_CLKS_NUM; i++) {
Tomasz Figa3966c942013-12-12 17:07:19 +0100208 struct clk_hw *hw = __clk_get_hw(clocks[i]);
209 struct max77686_clk *max77686 = to_max77686_clk(hw);
210
211 clkdev_drop(max77686->lookup);
212 clk_unregister(clocks[i]);
Jonghwa Lee73118e62012-08-28 17:54:28 +0900213 }
214 return 0;
215}
216
217static const struct platform_device_id max77686_clk_id[] = {
218 { "max77686-clk", 0},
219 { },
220};
221MODULE_DEVICE_TABLE(platform, max77686_clk_id);
222
223static struct platform_driver max77686_clk_driver = {
224 .driver = {
225 .name = "max77686-clk",
226 .owner = THIS_MODULE,
227 },
228 .probe = max77686_clk_probe,
Bill Pembertonf9cfa632012-11-19 13:19:59 -0500229 .remove = max77686_clk_remove,
Jonghwa Lee73118e62012-08-28 17:54:28 +0900230 .id_table = max77686_clk_id,
231};
232
233static int __init max77686_clk_init(void)
234{
235 return platform_driver_register(&max77686_clk_driver);
236}
237subsys_initcall(max77686_clk_init);
238
239static void __init max77686_clk_cleanup(void)
240{
241 platform_driver_unregister(&max77686_clk_driver);
242}
243module_exit(max77686_clk_cleanup);
244
245MODULE_DESCRIPTION("MAXIM 77686 Clock Driver");
246MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
247MODULE_LICENSE("GPL");