2 * Generic pwmlib implementation
4 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
5 * Copyright (C) 2011-2012 Avionic Design GmbH
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
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; see the file COPYING. If not, write to
19 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/pwm.h>
24 #include <linux/radix-tree.h>
25 #include <linux/list.h>
26 #include <linux/mutex.h>
27 #include <linux/err.h>
28 #include <linux/slab.h>
29 #include <linux/device.h>
33 static DEFINE_MUTEX(pwm_lock
);
34 static LIST_HEAD(pwm_chips
);
35 static DECLARE_BITMAP(allocated_pwms
, MAX_PWMS
);
36 static RADIX_TREE(pwm_tree
, GFP_KERNEL
);
38 static struct pwm_device
*pwm_to_device(unsigned int pwm
)
40 return radix_tree_lookup(&pwm_tree
, pwm
);
43 static int alloc_pwms(int pwm
, unsigned int count
)
45 unsigned int from
= 0;
54 start
= bitmap_find_next_zero_area(allocated_pwms
, MAX_PWMS
, from
,
57 if (pwm
>= 0 && start
!= pwm
)
60 if (start
+ count
> MAX_PWMS
)
66 static void free_pwms(struct pwm_chip
*chip
)
70 for (i
= 0; i
< chip
->npwm
; i
++) {
71 struct pwm_device
*pwm
= &chip
->pwms
[i
];
72 radix_tree_delete(&pwm_tree
, pwm
->pwm
);
75 bitmap_clear(allocated_pwms
, chip
->base
, chip
->npwm
);
81 static int pwm_device_request(struct pwm_device
*pwm
, const char *label
)
85 if (test_bit(PWMF_REQUESTED
, &pwm
->flags
))
88 if (!try_module_get(pwm
->chip
->ops
->owner
))
91 if (pwm
->chip
->ops
->request
) {
92 err
= pwm
->chip
->ops
->request(pwm
->chip
, pwm
);
94 module_put(pwm
->chip
->ops
->owner
);
99 set_bit(PWMF_REQUESTED
, &pwm
->flags
);
106 * pwm_set_chip_data() - set private chip data for a PWM
108 * @data: pointer to chip-specific data
110 int pwm_set_chip_data(struct pwm_device
*pwm
, void *data
)
115 pwm
->chip_data
= data
;
121 * pwm_get_chip_data() - get private chip data for a PWM
124 void *pwm_get_chip_data(struct pwm_device
*pwm
)
126 return pwm
? pwm
->chip_data
: NULL
;
130 * pwmchip_add() - register a new PWM chip
131 * @chip: the PWM chip to add
133 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
136 int pwmchip_add(struct pwm_chip
*chip
)
138 struct pwm_device
*pwm
;
142 if (!chip
|| !chip
->dev
|| !chip
->ops
|| !chip
->ops
->config
||
143 !chip
->ops
->enable
|| !chip
->ops
->disable
)
146 mutex_lock(&pwm_lock
);
148 ret
= alloc_pwms(chip
->base
, chip
->npwm
);
152 chip
->pwms
= kzalloc(chip
->npwm
* sizeof(*pwm
), GFP_KERNEL
);
160 for (i
= 0; i
< chip
->npwm
; i
++) {
161 pwm
= &chip
->pwms
[i
];
164 pwm
->pwm
= chip
->base
+ i
;
167 radix_tree_insert(&pwm_tree
, pwm
->pwm
, pwm
);
170 bitmap_set(allocated_pwms
, chip
->base
, chip
->npwm
);
172 INIT_LIST_HEAD(&chip
->list
);
173 list_add(&chip
->list
, &pwm_chips
);
178 mutex_unlock(&pwm_lock
);
181 EXPORT_SYMBOL_GPL(pwmchip_add
);
184 * pwmchip_remove() - remove a PWM chip
185 * @chip: the PWM chip to remove
187 * Removes a PWM chip. This function may return busy if the PWM chip provides
188 * a PWM device that is still requested.
190 int pwmchip_remove(struct pwm_chip
*chip
)
195 mutex_lock(&pwm_lock
);
197 for (i
= 0; i
< chip
->npwm
; i
++) {
198 struct pwm_device
*pwm
= &chip
->pwms
[i
];
200 if (test_bit(PWMF_REQUESTED
, &pwm
->flags
)) {
206 list_del_init(&chip
->list
);
210 mutex_unlock(&pwm_lock
);
213 EXPORT_SYMBOL_GPL(pwmchip_remove
);
216 * pwm_request() - request a PWM device
217 * @pwm_id: global PWM device index
218 * @label: PWM device label
220 struct pwm_device
*pwm_request(int pwm
, const char *label
)
222 struct pwm_device
*dev
;
225 if (pwm
< 0 || pwm
>= MAX_PWMS
)
226 return ERR_PTR(-EINVAL
);
228 mutex_lock(&pwm_lock
);
230 dev
= pwm_to_device(pwm
);
232 dev
= ERR_PTR(-EPROBE_DEFER
);
236 err
= pwm_device_request(dev
, label
);
241 mutex_unlock(&pwm_lock
);
245 EXPORT_SYMBOL_GPL(pwm_request
);
248 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
250 * @index: per-chip index of the PWM to request
251 * @label: a literal description string of this PWM
253 * Returns the PWM at the given index of the given PWM chip. A negative error
254 * code is returned if the index is not valid for the specified PWM chip or
255 * if the PWM device cannot be requested.
257 struct pwm_device
*pwm_request_from_chip(struct pwm_chip
*chip
,
261 struct pwm_device
*pwm
;
264 if (!chip
|| index
>= chip
->npwm
)
265 return ERR_PTR(-EINVAL
);
267 mutex_lock(&pwm_lock
);
268 pwm
= &chip
->pwms
[index
];
270 err
= pwm_device_request(pwm
, label
);
274 mutex_unlock(&pwm_lock
);
277 EXPORT_SYMBOL_GPL(pwm_request_from_chip
);
280 * pwm_free() - free a PWM device
283 void pwm_free(struct pwm_device
*pwm
)
285 mutex_lock(&pwm_lock
);
287 if (!test_and_clear_bit(PWMF_REQUESTED
, &pwm
->flags
)) {
288 pr_warning("PWM device already freed\n");
292 if (pwm
->chip
->ops
->free
)
293 pwm
->chip
->ops
->free(pwm
->chip
, pwm
);
297 module_put(pwm
->chip
->ops
->owner
);
299 mutex_unlock(&pwm_lock
);
301 EXPORT_SYMBOL_GPL(pwm_free
);
304 * pwm_config() - change a PWM device configuration
306 * @duty_ns: "on" time (in nanoseconds)
307 * @period_ns: duration (in nanoseconds) of one cycle
309 int pwm_config(struct pwm_device
*pwm
, int duty_ns
, int period_ns
)
311 if (!pwm
|| period_ns
== 0 || duty_ns
> period_ns
)
314 return pwm
->chip
->ops
->config(pwm
->chip
, pwm
, duty_ns
, period_ns
);
316 EXPORT_SYMBOL_GPL(pwm_config
);
319 * pwm_enable() - start a PWM output toggling
322 int pwm_enable(struct pwm_device
*pwm
)
324 if (pwm
&& !test_and_set_bit(PWMF_ENABLED
, &pwm
->flags
))
325 return pwm
->chip
->ops
->enable(pwm
->chip
, pwm
);
327 return pwm
? 0 : -EINVAL
;
329 EXPORT_SYMBOL_GPL(pwm_enable
);
332 * pwm_disable() - stop a PWM output toggling
335 void pwm_disable(struct pwm_device
*pwm
)
337 if (pwm
&& test_and_clear_bit(PWMF_ENABLED
, &pwm
->flags
))
338 pwm
->chip
->ops
->disable(pwm
->chip
, pwm
);
340 EXPORT_SYMBOL_GPL(pwm_disable
);
This page took 0.049265 seconds and 5 git commands to generate.