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
*get_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
;
57 max77686
= get_max77686_clk(hw
);
61 ret
= regmap_update_bits(max77686
->iodev
->regmap
,
62 MAX77686_REG_32KHZ
, max77686
->mask
, max77686
->mask
);
67 static void max77686_clk_unprepare(struct clk_hw
*hw
)
69 struct max77686_clk
*max77686
;
71 max77686
= get_max77686_clk(hw
);
75 regmap_update_bits(max77686
->iodev
->regmap
,
76 MAX77686_REG_32KHZ
, max77686
->mask
, ~max77686
->mask
);
79 static int max77686_clk_is_enabled(struct clk_hw
*hw
)
81 struct max77686_clk
*max77686
;
85 max77686
= get_max77686_clk(hw
);
89 ret
= regmap_read(max77686
->iodev
->regmap
,
90 MAX77686_REG_32KHZ
, &val
);
95 return val
& max77686
->mask
;
98 static struct clk_ops max77686_clk_ops
= {
99 .prepare
= max77686_clk_prepare
,
100 .unprepare
= max77686_clk_unprepare
,
101 .is_enabled
= max77686_clk_is_enabled
,
104 static struct clk_init_data max77686_clks_init
[MAX77686_CLKS_NUM
] = {
105 [MAX77686_CLK_AP
] = {
107 .ops
= &max77686_clk_ops
,
108 .flags
= CLK_IS_ROOT
,
110 [MAX77686_CLK_CP
] = {
112 .ops
= &max77686_clk_ops
,
113 .flags
= CLK_IS_ROOT
,
115 [MAX77686_CLK_PMIC
] = {
116 .name
= "32khz_pmic",
117 .ops
= &max77686_clk_ops
,
118 .flags
= CLK_IS_ROOT
,
122 static int max77686_clk_register(struct device
*dev
,
123 struct max77686_clk
*max77686
)
126 struct clk_hw
*hw
= &max77686
->hw
;
128 clk
= clk_register(dev
, hw
);
133 max77686
->lookup
= devm_kzalloc(dev
, sizeof(struct clk_lookup
),
135 if (IS_ERR(max77686
->lookup
))
138 max77686
->lookup
->con_id
= hw
->init
->name
;
139 max77686
->lookup
->clk
= clk
;
141 clkdev_add(max77686
->lookup
);
146 static int max77686_clk_probe(struct platform_device
*pdev
)
148 struct max77686_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
149 struct max77686_clk
**max77686_clks
;
152 max77686_clks
= devm_kzalloc(&pdev
->dev
, sizeof(struct max77686_clk
*)
153 * MAX77686_CLKS_NUM
, GFP_KERNEL
);
154 if (IS_ERR(max77686_clks
))
157 for (i
= 0; i
< MAX77686_CLKS_NUM
; i
++) {
158 max77686_clks
[i
] = devm_kzalloc(&pdev
->dev
,
159 sizeof(struct max77686_clk
), GFP_KERNEL
);
160 if (IS_ERR(max77686_clks
[i
]))
164 for (i
= 0; i
< MAX77686_CLKS_NUM
; i
++) {
165 max77686_clks
[i
]->iodev
= iodev
;
166 max77686_clks
[i
]->mask
= 1 << i
;
167 max77686_clks
[i
]->hw
.init
= &max77686_clks_init
[i
];
169 ret
= max77686_clk_register(&pdev
->dev
, max77686_clks
[i
]);
172 case MAX77686_CLK_AP
:
173 dev_err(&pdev
->dev
, "Fail to register CLK_AP\n");
176 case MAX77686_CLK_CP
:
177 dev_err(&pdev
->dev
, "Fail to register CLK_CP\n");
180 case MAX77686_CLK_PMIC
:
181 dev_err(&pdev
->dev
, "Fail to register CLK_PMIC\n");
187 platform_set_drvdata(pdev
, max77686_clks
);
192 clkdev_drop(max77686_clks
[MAX77686_CLK_CP
]->lookup
);
193 kfree(max77686_clks
[MAX77686_CLK_CP
]->hw
.clk
);
195 clkdev_drop(max77686_clks
[MAX77686_CLK_AP
]->lookup
);
196 kfree(max77686_clks
[MAX77686_CLK_AP
]->hw
.clk
);
202 static int max77686_clk_remove(struct platform_device
*pdev
)
204 struct max77686_clk
**max77686_clks
= platform_get_drvdata(pdev
);
207 for (i
= 0; i
< MAX77686_CLKS_NUM
; i
++) {
208 clkdev_drop(max77686_clks
[i
]->lookup
);
209 kfree(max77686_clks
[i
]->hw
.clk
);
214 static const struct platform_device_id max77686_clk_id
[] = {
215 { "max77686-clk", 0},
218 MODULE_DEVICE_TABLE(platform
, max77686_clk_id
);
220 static struct platform_driver max77686_clk_driver
= {
222 .name
= "max77686-clk",
223 .owner
= THIS_MODULE
,
225 .probe
= max77686_clk_probe
,
226 .remove
= max77686_clk_remove
,
227 .id_table
= max77686_clk_id
,
230 static int __init
max77686_clk_init(void)
232 return platform_driver_register(&max77686_clk_driver
);
234 subsys_initcall(max77686_clk_init
);
236 static void __init
max77686_clk_cleanup(void)
238 platform_driver_unregister(&max77686_clk_driver
);
240 module_exit(max77686_clk_cleanup
);
242 MODULE_DESCRIPTION("MAXIM 77686 Clock Driver");
243 MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
244 MODULE_LICENSE("GPL");