4 * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/atomic.h>
12 #include <linux/clk.h>
13 #include <linux/device.h>
14 #include <linux/errno.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
21 #include <media/v4l2-clk.h>
22 #include <media/v4l2-subdev.h>
24 static DEFINE_MUTEX(clk_lock
);
25 static LIST_HEAD(clk_list
);
27 static struct v4l2_clk
*v4l2_clk_find(const char *dev_id
)
31 list_for_each_entry(clk
, &clk_list
, list
)
32 if (!strcmp(dev_id
, clk
->dev_id
))
35 return ERR_PTR(-ENODEV
);
38 struct v4l2_clk
*v4l2_clk_get(struct device
*dev
, const char *id
)
41 struct clk
*ccf_clk
= clk_get(dev
, id
);
43 if (PTR_ERR(ccf_clk
) == -EPROBE_DEFER
)
44 return ERR_PTR(-EPROBE_DEFER
);
46 if (!IS_ERR_OR_NULL(ccf_clk
)) {
47 clk
= kzalloc(sizeof(*clk
), GFP_KERNEL
);
50 return ERR_PTR(-ENOMEM
);
57 mutex_lock(&clk_lock
);
58 clk
= v4l2_clk_find(dev_name(dev
));
61 atomic_inc(&clk
->use_count
);
62 mutex_unlock(&clk_lock
);
66 EXPORT_SYMBOL(v4l2_clk_get
);
68 void v4l2_clk_put(struct v4l2_clk
*clk
)
81 mutex_lock(&clk_lock
);
83 list_for_each_entry(tmp
, &clk_list
, list
)
85 atomic_dec(&clk
->use_count
);
87 mutex_unlock(&clk_lock
);
89 EXPORT_SYMBOL(v4l2_clk_put
);
91 static int v4l2_clk_lock_driver(struct v4l2_clk
*clk
)
96 mutex_lock(&clk_lock
);
98 list_for_each_entry(tmp
, &clk_list
, list
)
100 ret
= !try_module_get(clk
->ops
->owner
);
106 mutex_unlock(&clk_lock
);
111 static void v4l2_clk_unlock_driver(struct v4l2_clk
*clk
)
113 module_put(clk
->ops
->owner
);
116 int v4l2_clk_enable(struct v4l2_clk
*clk
)
121 return clk_prepare_enable(clk
->clk
);
123 ret
= v4l2_clk_lock_driver(clk
);
127 mutex_lock(&clk
->lock
);
129 if (++clk
->enable
== 1 && clk
->ops
->enable
) {
130 ret
= clk
->ops
->enable(clk
);
135 mutex_unlock(&clk
->lock
);
139 EXPORT_SYMBOL(v4l2_clk_enable
);
142 * You might Oops if you try to disabled a disabled clock, because then the
143 * driver isn't locked and could have been unloaded by now, so, don't do that
145 void v4l2_clk_disable(struct v4l2_clk
*clk
)
150 return clk_disable_unprepare(clk
->clk
);
152 mutex_lock(&clk
->lock
);
154 enable
= --clk
->enable
;
155 if (WARN(enable
< 0, "Unbalanced %s() on %s!\n", __func__
,
158 else if (!enable
&& clk
->ops
->disable
)
159 clk
->ops
->disable(clk
);
161 mutex_unlock(&clk
->lock
);
163 v4l2_clk_unlock_driver(clk
);
165 EXPORT_SYMBOL(v4l2_clk_disable
);
167 unsigned long v4l2_clk_get_rate(struct v4l2_clk
*clk
)
172 return clk_get_rate(clk
->clk
);
174 ret
= v4l2_clk_lock_driver(clk
);
178 mutex_lock(&clk
->lock
);
179 if (!clk
->ops
->get_rate
)
182 ret
= clk
->ops
->get_rate(clk
);
183 mutex_unlock(&clk
->lock
);
185 v4l2_clk_unlock_driver(clk
);
189 EXPORT_SYMBOL(v4l2_clk_get_rate
);
191 int v4l2_clk_set_rate(struct v4l2_clk
*clk
, unsigned long rate
)
196 long r
= clk_round_rate(clk
->clk
, rate
);
199 return clk_set_rate(clk
->clk
, r
);
202 ret
= v4l2_clk_lock_driver(clk
);
207 mutex_lock(&clk
->lock
);
208 if (!clk
->ops
->set_rate
)
211 ret
= clk
->ops
->set_rate(clk
, rate
);
212 mutex_unlock(&clk
->lock
);
214 v4l2_clk_unlock_driver(clk
);
218 EXPORT_SYMBOL(v4l2_clk_set_rate
);
220 struct v4l2_clk
*v4l2_clk_register(const struct v4l2_clk_ops
*ops
,
224 struct v4l2_clk
*clk
;
228 return ERR_PTR(-EINVAL
);
230 clk
= kzalloc(sizeof(struct v4l2_clk
), GFP_KERNEL
);
232 return ERR_PTR(-ENOMEM
);
234 clk
->dev_id
= kstrdup(dev_id
, GFP_KERNEL
);
241 atomic_set(&clk
->use_count
, 0);
242 mutex_init(&clk
->lock
);
244 mutex_lock(&clk_lock
);
245 if (!IS_ERR(v4l2_clk_find(dev_id
))) {
246 mutex_unlock(&clk_lock
);
250 list_add_tail(&clk
->list
, &clk_list
);
251 mutex_unlock(&clk_lock
);
261 EXPORT_SYMBOL(v4l2_clk_register
);
263 void v4l2_clk_unregister(struct v4l2_clk
*clk
)
265 if (WARN(atomic_read(&clk
->use_count
),
266 "%s(): Refusing to unregister ref-counted %s clock!\n",
267 __func__
, clk
->dev_id
))
270 mutex_lock(&clk_lock
);
271 list_del(&clk
->list
);
272 mutex_unlock(&clk_lock
);
277 EXPORT_SYMBOL(v4l2_clk_unregister
);
279 struct v4l2_clk_fixed
{
281 struct v4l2_clk_ops ops
;
284 static unsigned long fixed_get_rate(struct v4l2_clk
*clk
)
286 struct v4l2_clk_fixed
*priv
= clk
->priv
;
290 struct v4l2_clk
*__v4l2_clk_register_fixed(const char *dev_id
,
291 unsigned long rate
, struct module
*owner
)
293 struct v4l2_clk
*clk
;
294 struct v4l2_clk_fixed
*priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
297 return ERR_PTR(-ENOMEM
);
300 priv
->ops
.get_rate
= fixed_get_rate
;
301 priv
->ops
.owner
= owner
;
303 clk
= v4l2_clk_register(&priv
->ops
, dev_id
, priv
);
309 EXPORT_SYMBOL(__v4l2_clk_register_fixed
);
311 void v4l2_clk_unregister_fixed(struct v4l2_clk
*clk
)
314 v4l2_clk_unregister(clk
);
316 EXPORT_SYMBOL(v4l2_clk_unregister_fixed
);