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/device.h>
13 #include <linux/errno.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
20 #include <media/v4l2-clk.h>
21 #include <media/v4l2-subdev.h>
23 static DEFINE_MUTEX(clk_lock
);
24 static LIST_HEAD(clk_list
);
26 static struct v4l2_clk
*v4l2_clk_find(const char *dev_id
, const char *id
)
30 list_for_each_entry(clk
, &clk_list
, list
) {
31 if (strcmp(dev_id
, clk
->dev_id
))
34 if (!id
|| !clk
->id
|| !strcmp(clk
->id
, id
))
38 return ERR_PTR(-ENODEV
);
41 struct v4l2_clk
*v4l2_clk_get(struct device
*dev
, const char *id
)
45 mutex_lock(&clk_lock
);
46 clk
= v4l2_clk_find(dev_name(dev
), id
);
49 atomic_inc(&clk
->use_count
);
50 mutex_unlock(&clk_lock
);
54 EXPORT_SYMBOL(v4l2_clk_get
);
56 void v4l2_clk_put(struct v4l2_clk
*clk
)
63 mutex_lock(&clk_lock
);
65 list_for_each_entry(tmp
, &clk_list
, list
)
67 atomic_dec(&clk
->use_count
);
69 mutex_unlock(&clk_lock
);
71 EXPORT_SYMBOL(v4l2_clk_put
);
73 static int v4l2_clk_lock_driver(struct v4l2_clk
*clk
)
78 mutex_lock(&clk_lock
);
80 list_for_each_entry(tmp
, &clk_list
, list
)
82 ret
= !try_module_get(clk
->ops
->owner
);
88 mutex_unlock(&clk_lock
);
93 static void v4l2_clk_unlock_driver(struct v4l2_clk
*clk
)
95 module_put(clk
->ops
->owner
);
98 int v4l2_clk_enable(struct v4l2_clk
*clk
)
100 int ret
= v4l2_clk_lock_driver(clk
);
105 mutex_lock(&clk
->lock
);
107 if (++clk
->enable
== 1 && clk
->ops
->enable
) {
108 ret
= clk
->ops
->enable(clk
);
113 mutex_unlock(&clk
->lock
);
117 EXPORT_SYMBOL(v4l2_clk_enable
);
120 * You might Oops if you try to disabled a disabled clock, because then the
121 * driver isn't locked and could have been unloaded by now, so, don't do that
123 void v4l2_clk_disable(struct v4l2_clk
*clk
)
127 mutex_lock(&clk
->lock
);
129 enable
= --clk
->enable
;
130 if (WARN(enable
< 0, "Unbalanced %s() on %s:%s!\n", __func__
,
131 clk
->dev_id
, clk
->id
))
133 else if (!enable
&& clk
->ops
->disable
)
134 clk
->ops
->disable(clk
);
136 mutex_unlock(&clk
->lock
);
138 v4l2_clk_unlock_driver(clk
);
140 EXPORT_SYMBOL(v4l2_clk_disable
);
142 unsigned long v4l2_clk_get_rate(struct v4l2_clk
*clk
)
144 int ret
= v4l2_clk_lock_driver(clk
);
149 mutex_lock(&clk
->lock
);
150 if (!clk
->ops
->get_rate
)
153 ret
= clk
->ops
->get_rate(clk
);
154 mutex_unlock(&clk
->lock
);
156 v4l2_clk_unlock_driver(clk
);
160 EXPORT_SYMBOL(v4l2_clk_get_rate
);
162 int v4l2_clk_set_rate(struct v4l2_clk
*clk
, unsigned long rate
)
164 int ret
= v4l2_clk_lock_driver(clk
);
169 mutex_lock(&clk
->lock
);
170 if (!clk
->ops
->set_rate
)
173 ret
= clk
->ops
->set_rate(clk
, rate
);
174 mutex_unlock(&clk
->lock
);
176 v4l2_clk_unlock_driver(clk
);
180 EXPORT_SYMBOL(v4l2_clk_set_rate
);
182 struct v4l2_clk
*v4l2_clk_register(const struct v4l2_clk_ops
*ops
,
184 const char *id
, void *priv
)
186 struct v4l2_clk
*clk
;
190 return ERR_PTR(-EINVAL
);
192 clk
= kzalloc(sizeof(struct v4l2_clk
), GFP_KERNEL
);
194 return ERR_PTR(-ENOMEM
);
196 clk
->id
= kstrdup(id
, GFP_KERNEL
);
197 clk
->dev_id
= kstrdup(dev_id
, GFP_KERNEL
);
198 if ((id
&& !clk
->id
) || !clk
->dev_id
) {
204 atomic_set(&clk
->use_count
, 0);
205 mutex_init(&clk
->lock
);
207 mutex_lock(&clk_lock
);
208 if (!IS_ERR(v4l2_clk_find(dev_id
, id
))) {
209 mutex_unlock(&clk_lock
);
213 list_add_tail(&clk
->list
, &clk_list
);
214 mutex_unlock(&clk_lock
);
225 EXPORT_SYMBOL(v4l2_clk_register
);
227 void v4l2_clk_unregister(struct v4l2_clk
*clk
)
229 if (WARN(atomic_read(&clk
->use_count
),
230 "%s(): Refusing to unregister ref-counted %s:%s clock!\n",
231 __func__
, clk
->dev_id
, clk
->id
))
234 mutex_lock(&clk_lock
);
235 list_del(&clk
->list
);
236 mutex_unlock(&clk_lock
);
242 EXPORT_SYMBOL(v4l2_clk_unregister
);
244 struct v4l2_clk_fixed
{
246 struct v4l2_clk_ops ops
;
249 static unsigned long fixed_get_rate(struct v4l2_clk
*clk
)
251 struct v4l2_clk_fixed
*priv
= clk
->priv
;
255 struct v4l2_clk
*__v4l2_clk_register_fixed(const char *dev_id
,
256 const char *id
, unsigned long rate
, struct module
*owner
)
258 struct v4l2_clk
*clk
;
259 struct v4l2_clk_fixed
*priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
262 return ERR_PTR(-ENOMEM
);
265 priv
->ops
.get_rate
= fixed_get_rate
;
266 priv
->ops
.owner
= owner
;
268 clk
= v4l2_clk_register(&priv
->ops
, dev_id
, id
, priv
);
274 EXPORT_SYMBOL(__v4l2_clk_register_fixed
);
276 void v4l2_clk_unregister_fixed(struct v4l2_clk
*clk
)
279 v4l2_clk_unregister(clk
);
281 EXPORT_SYMBOL(v4l2_clk_unregister_fixed
);