blob: a2af599e61ae3434b174286df47eb622742724da [file] [log] [blame]
Sascha Hauer0c2498f2011-01-28 09:40:40 +01001/*
2 * Generic pwmlib implementation
3 *
4 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
Thierry Redingf051c462011-12-14 11:12:23 +01005 * Copyright (C) 2011-2012 Avionic Design GmbH
Sascha Hauer0c2498f2011-01-28 09:40:40 +01006 *
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)
10 * 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; see the file COPYING. If not, write to
19 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/module.h>
23#include <linux/pwm.h>
Thierry Redingf051c462011-12-14 11:12:23 +010024#include <linux/radix-tree.h>
Sascha Hauer0c2498f2011-01-28 09:40:40 +010025#include <linux/list.h>
26#include <linux/mutex.h>
27#include <linux/err.h>
28#include <linux/slab.h>
29#include <linux/device.h>
Thierry Reding62099abf2012-03-26 09:31:48 +020030#include <linux/debugfs.h>
31#include <linux/seq_file.h>
Sascha Hauer0c2498f2011-01-28 09:40:40 +010032
Thierry Redingf051c462011-12-14 11:12:23 +010033#define MAX_PWMS 1024
Sascha Hauer0c2498f2011-01-28 09:40:40 +010034
Thierry Reding8138d2d2012-03-26 08:42:48 +020035static DEFINE_MUTEX(pwm_lookup_lock);
36static LIST_HEAD(pwm_lookup_list);
Sascha Hauer0c2498f2011-01-28 09:40:40 +010037static DEFINE_MUTEX(pwm_lock);
Thierry Redingf051c462011-12-14 11:12:23 +010038static LIST_HEAD(pwm_chips);
39static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
40static RADIX_TREE(pwm_tree, GFP_KERNEL);
Sascha Hauer0c2498f2011-01-28 09:40:40 +010041
Thierry Redingf051c462011-12-14 11:12:23 +010042static struct pwm_device *pwm_to_device(unsigned int pwm)
Sascha Hauer0c2498f2011-01-28 09:40:40 +010043{
Thierry Redingf051c462011-12-14 11:12:23 +010044 return radix_tree_lookup(&pwm_tree, pwm);
45}
Sascha Hauer0c2498f2011-01-28 09:40:40 +010046
Thierry Redingf051c462011-12-14 11:12:23 +010047static int alloc_pwms(int pwm, unsigned int count)
48{
49 unsigned int from = 0;
50 unsigned int start;
51
52 if (pwm >= MAX_PWMS)
53 return -EINVAL;
54
55 if (pwm >= 0)
56 from = pwm;
57
58 start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from,
59 count, 0);
60
61 if (pwm >= 0 && start != pwm)
62 return -EEXIST;
63
64 if (start + count > MAX_PWMS)
65 return -ENOSPC;
66
67 return start;
68}
69
70static void free_pwms(struct pwm_chip *chip)
71{
72 unsigned int i;
73
74 for (i = 0; i < chip->npwm; i++) {
75 struct pwm_device *pwm = &chip->pwms[i];
76 radix_tree_delete(&pwm_tree, pwm->pwm);
Sascha Hauer0c2498f2011-01-28 09:40:40 +010077 }
78
Thierry Redingf051c462011-12-14 11:12:23 +010079 bitmap_clear(allocated_pwms, chip->base, chip->npwm);
80
81 kfree(chip->pwms);
82 chip->pwms = NULL;
83}
84
Thierry Reding8138d2d2012-03-26 08:42:48 +020085static struct pwm_chip *pwmchip_find_by_name(const char *name)
86{
87 struct pwm_chip *chip;
88
89 if (!name)
90 return NULL;
91
92 mutex_lock(&pwm_lock);
93
94 list_for_each_entry(chip, &pwm_chips, list) {
95 const char *chip_name = dev_name(chip->dev);
96
97 if (chip_name && strcmp(chip_name, name) == 0) {
98 mutex_unlock(&pwm_lock);
99 return chip;
100 }
101 }
102
103 mutex_unlock(&pwm_lock);
104
105 return NULL;
106}
107
Thierry Redingf051c462011-12-14 11:12:23 +0100108static int pwm_device_request(struct pwm_device *pwm, const char *label)
109{
110 int err;
111
112 if (test_bit(PWMF_REQUESTED, &pwm->flags))
113 return -EBUSY;
114
115 if (!try_module_get(pwm->chip->ops->owner))
116 return -ENODEV;
117
118 if (pwm->chip->ops->request) {
119 err = pwm->chip->ops->request(pwm->chip, pwm);
120 if (err) {
121 module_put(pwm->chip->ops->owner);
122 return err;
123 }
124 }
125
126 set_bit(PWMF_REQUESTED, &pwm->flags);
127 pwm->label = label;
128
129 return 0;
130}
131
132/**
133 * pwm_set_chip_data() - set private chip data for a PWM
134 * @pwm: PWM device
135 * @data: pointer to chip-specific data
136 */
137int pwm_set_chip_data(struct pwm_device *pwm, void *data)
138{
139 if (!pwm)
140 return -EINVAL;
141
142 pwm->chip_data = data;
143
144 return 0;
145}
146
147/**
148 * pwm_get_chip_data() - get private chip data for a PWM
149 * @pwm: PWM device
150 */
151void *pwm_get_chip_data(struct pwm_device *pwm)
152{
153 return pwm ? pwm->chip_data : NULL;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100154}
155
156/**
157 * pwmchip_add() - register a new PWM chip
158 * @chip: the PWM chip to add
Thierry Redingf051c462011-12-14 11:12:23 +0100159 *
160 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
161 * will be used.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100162 */
163int pwmchip_add(struct pwm_chip *chip)
164{
165 struct pwm_device *pwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100166 unsigned int i;
167 int ret;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100168
Thierry Redingf051c462011-12-14 11:12:23 +0100169 if (!chip || !chip->dev || !chip->ops || !chip->ops->config ||
170 !chip->ops->enable || !chip->ops->disable)
171 return -EINVAL;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100172
173 mutex_lock(&pwm_lock);
174
Thierry Redingf051c462011-12-14 11:12:23 +0100175 ret = alloc_pwms(chip->base, chip->npwm);
176 if (ret < 0)
177 goto out;
178
179 chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
180 if (!chip->pwms) {
181 ret = -ENOMEM;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100182 goto out;
183 }
184
Thierry Redingf051c462011-12-14 11:12:23 +0100185 chip->base = ret;
186
187 for (i = 0; i < chip->npwm; i++) {
188 pwm = &chip->pwms[i];
189
190 pwm->chip = chip;
191 pwm->pwm = chip->base + i;
192 pwm->hwpwm = i;
193
194 radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
195 }
196
197 bitmap_set(allocated_pwms, chip->base, chip->npwm);
198
199 INIT_LIST_HEAD(&chip->list);
200 list_add(&chip->list, &pwm_chips);
201
202 ret = 0;
203
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100204out:
205 mutex_unlock(&pwm_lock);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100206 return ret;
207}
208EXPORT_SYMBOL_GPL(pwmchip_add);
209
210/**
211 * pwmchip_remove() - remove a PWM chip
212 * @chip: the PWM chip to remove
213 *
214 * Removes a PWM chip. This function may return busy if the PWM chip provides
215 * a PWM device that is still requested.
216 */
217int pwmchip_remove(struct pwm_chip *chip)
218{
Thierry Redingf051c462011-12-14 11:12:23 +0100219 unsigned int i;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100220 int ret = 0;
221
222 mutex_lock(&pwm_lock);
223
Thierry Redingf051c462011-12-14 11:12:23 +0100224 for (i = 0; i < chip->npwm; i++) {
225 struct pwm_device *pwm = &chip->pwms[i];
226
227 if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
228 ret = -EBUSY;
229 goto out;
230 }
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100231 }
232
Thierry Redingf051c462011-12-14 11:12:23 +0100233 list_del_init(&chip->list);
234 free_pwms(chip);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100235
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100236out:
237 mutex_unlock(&pwm_lock);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100238 return ret;
239}
240EXPORT_SYMBOL_GPL(pwmchip_remove);
241
242/**
243 * pwm_request() - request a PWM device
244 * @pwm_id: global PWM device index
245 * @label: PWM device label
Thierry Reding8138d2d2012-03-26 08:42:48 +0200246 *
247 * This function is deprecated, use pwm_get() instead.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100248 */
Thierry Redingf051c462011-12-14 11:12:23 +0100249struct pwm_device *pwm_request(int pwm, const char *label)
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100250{
Thierry Redingf051c462011-12-14 11:12:23 +0100251 struct pwm_device *dev;
252 int err;
253
254 if (pwm < 0 || pwm >= MAX_PWMS)
255 return ERR_PTR(-EINVAL);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100256
257 mutex_lock(&pwm_lock);
258
Thierry Redingf051c462011-12-14 11:12:23 +0100259 dev = pwm_to_device(pwm);
260 if (!dev) {
261 dev = ERR_PTR(-EPROBE_DEFER);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100262 goto out;
263 }
264
Thierry Redingf051c462011-12-14 11:12:23 +0100265 err = pwm_device_request(dev, label);
266 if (err < 0)
267 dev = ERR_PTR(err);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100268
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100269out:
270 mutex_unlock(&pwm_lock);
271
Thierry Redingf051c462011-12-14 11:12:23 +0100272 return dev;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100273}
274EXPORT_SYMBOL_GPL(pwm_request);
275
276/**
Thierry Redingf051c462011-12-14 11:12:23 +0100277 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
278 * @chip: PWM chip
279 * @index: per-chip index of the PWM to request
280 * @label: a literal description string of this PWM
281 *
282 * Returns the PWM at the given index of the given PWM chip. A negative error
283 * code is returned if the index is not valid for the specified PWM chip or
284 * if the PWM device cannot be requested.
285 */
286struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
287 unsigned int index,
288 const char *label)
289{
290 struct pwm_device *pwm;
291 int err;
292
293 if (!chip || index >= chip->npwm)
294 return ERR_PTR(-EINVAL);
295
296 mutex_lock(&pwm_lock);
297 pwm = &chip->pwms[index];
298
299 err = pwm_device_request(pwm, label);
300 if (err < 0)
301 pwm = ERR_PTR(err);
302
303 mutex_unlock(&pwm_lock);
304 return pwm;
305}
306EXPORT_SYMBOL_GPL(pwm_request_from_chip);
307
308/**
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100309 * pwm_free() - free a PWM device
310 * @pwm: PWM device
Thierry Reding8138d2d2012-03-26 08:42:48 +0200311 *
312 * This function is deprecated, use pwm_put() instead.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100313 */
314void pwm_free(struct pwm_device *pwm)
315{
Thierry Reding8138d2d2012-03-26 08:42:48 +0200316 pwm_put(pwm);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100317}
318EXPORT_SYMBOL_GPL(pwm_free);
319
320/**
321 * pwm_config() - change a PWM device configuration
322 * @pwm: PWM device
323 * @duty_ns: "on" time (in nanoseconds)
324 * @period_ns: duration (in nanoseconds) of one cycle
325 */
326int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
327{
Thierry Redingf051c462011-12-14 11:12:23 +0100328 if (!pwm || period_ns == 0 || duty_ns > period_ns)
329 return -EINVAL;
330
331 return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100332}
333EXPORT_SYMBOL_GPL(pwm_config);
334
335/**
336 * pwm_enable() - start a PWM output toggling
337 * @pwm: PWM device
338 */
339int pwm_enable(struct pwm_device *pwm)
340{
Thierry Redingf051c462011-12-14 11:12:23 +0100341 if (pwm && !test_and_set_bit(PWMF_ENABLED, &pwm->flags))
342 return pwm->chip->ops->enable(pwm->chip, pwm);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100343
Thierry Redingf051c462011-12-14 11:12:23 +0100344 return pwm ? 0 : -EINVAL;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100345}
346EXPORT_SYMBOL_GPL(pwm_enable);
347
348/**
349 * pwm_disable() - stop a PWM output toggling
350 * @pwm: PWM device
351 */
352void pwm_disable(struct pwm_device *pwm)
353{
Thierry Redingf051c462011-12-14 11:12:23 +0100354 if (pwm && test_and_clear_bit(PWMF_ENABLED, &pwm->flags))
355 pwm->chip->ops->disable(pwm->chip, pwm);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100356}
357EXPORT_SYMBOL_GPL(pwm_disable);
Thierry Reding62099abf2012-03-26 09:31:48 +0200358
Thierry Reding8138d2d2012-03-26 08:42:48 +0200359/**
360 * pwm_add_table() - register PWM device consumers
361 * @table: array of consumers to register
362 * @num: number of consumers in table
363 */
364void __init pwm_add_table(struct pwm_lookup *table, size_t num)
365{
366 mutex_lock(&pwm_lookup_lock);
367
368 while (num--) {
369 list_add_tail(&table->list, &pwm_lookup_list);
370 table++;
371 }
372
373 mutex_unlock(&pwm_lookup_lock);
374}
375
376/**
377 * pwm_get() - look up and request a PWM device
378 * @dev: device for PWM consumer
379 * @con_id: consumer name
380 *
381 * Look up a PWM chip and a relative index via a table supplied by board setup
382 * code (see pwm_add_table()).
383 *
384 * Once a PWM chip has been found the specified PWM device will be requested
385 * and is ready to be used.
386 */
387struct pwm_device *pwm_get(struct device *dev, const char *con_id)
388{
389 struct pwm_device *pwm = ERR_PTR(-EPROBE_DEFER);
390 const char *dev_id = dev ? dev_name(dev): NULL;
391 struct pwm_chip *chip = NULL;
392 unsigned int best = 0;
393 struct pwm_lookup *p;
394 unsigned int index;
395 unsigned int match;
396
397 /*
398 * We look up the provider in the static table typically provided by
399 * board setup code. We first try to lookup the consumer device by
400 * name. If the consumer device was passed in as NULL or if no match
401 * was found, we try to find the consumer by directly looking it up
402 * by name.
403 *
404 * If a match is found, the provider PWM chip is looked up by name
405 * and a PWM device is requested using the PWM device per-chip index.
406 *
407 * The lookup algorithm was shamelessly taken from the clock
408 * framework:
409 *
410 * We do slightly fuzzy matching here:
411 * An entry with a NULL ID is assumed to be a wildcard.
412 * If an entry has a device ID, it must match
413 * If an entry has a connection ID, it must match
414 * Then we take the most specific entry - with the following order
415 * of precedence: dev+con > dev only > con only.
416 */
417 mutex_lock(&pwm_lookup_lock);
418
419 list_for_each_entry(p, &pwm_lookup_list, list) {
420 match = 0;
421
422 if (p->dev_id) {
423 if (!dev_id || strcmp(p->dev_id, dev_id))
424 continue;
425
426 match += 2;
427 }
428
429 if (p->con_id) {
430 if (!con_id || strcmp(p->con_id, con_id))
431 continue;
432
433 match += 1;
434 }
435
436 if (match > best) {
437 chip = pwmchip_find_by_name(p->provider);
438 index = p->index;
439
440 if (match != 3)
441 best = match;
442 else
443 break;
444 }
445 }
446
447 if (chip)
448 pwm = pwm_request_from_chip(chip, index, con_id ?: dev_id);
449
450 mutex_unlock(&pwm_lookup_lock);
451
452 return pwm;
453}
454EXPORT_SYMBOL_GPL(pwm_get);
455
456/**
457 * pwm_put() - release a PWM device
458 * @pwm: PWM device
459 */
460void pwm_put(struct pwm_device *pwm)
461{
462 if (!pwm)
463 return;
464
465 mutex_lock(&pwm_lock);
466
467 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
468 pr_warning("PWM device already freed\n");
469 goto out;
470 }
471
472 if (pwm->chip->ops->free)
473 pwm->chip->ops->free(pwm->chip, pwm);
474
475 pwm->label = NULL;
476
477 module_put(pwm->chip->ops->owner);
478out:
479 mutex_unlock(&pwm_lock);
480}
481EXPORT_SYMBOL_GPL(pwm_put);
482
Thierry Reding62099abf2012-03-26 09:31:48 +0200483#ifdef CONFIG_DEBUG_FS
484static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
485{
486 unsigned int i;
487
488 for (i = 0; i < chip->npwm; i++) {
489 struct pwm_device *pwm = &chip->pwms[i];
490
491 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
492
493 if (test_bit(PWMF_REQUESTED, &pwm->flags))
494 seq_printf(s, " requested");
495
496 if (test_bit(PWMF_ENABLED, &pwm->flags))
497 seq_printf(s, " enabled");
498
499 seq_printf(s, "\n");
500 }
501}
502
503static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
504{
505 mutex_lock(&pwm_lock);
506 s->private = "";
507
508 return seq_list_start(&pwm_chips, *pos);
509}
510
511static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
512{
513 s->private = "\n";
514
515 return seq_list_next(v, &pwm_chips, pos);
516}
517
518static void pwm_seq_stop(struct seq_file *s, void *v)
519{
520 mutex_unlock(&pwm_lock);
521}
522
523static int pwm_seq_show(struct seq_file *s, void *v)
524{
525 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
526
527 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
528 chip->dev->bus ? chip->dev->bus->name : "no-bus",
529 dev_name(chip->dev), chip->npwm,
530 (chip->npwm != 1) ? "s" : "");
531
532 if (chip->ops->dbg_show)
533 chip->ops->dbg_show(chip, s);
534 else
535 pwm_dbg_show(chip, s);
536
537 return 0;
538}
539
540static const struct seq_operations pwm_seq_ops = {
541 .start = pwm_seq_start,
542 .next = pwm_seq_next,
543 .stop = pwm_seq_stop,
544 .show = pwm_seq_show,
545};
546
547static int pwm_seq_open(struct inode *inode, struct file *file)
548{
549 return seq_open(file, &pwm_seq_ops);
550}
551
552static const struct file_operations pwm_debugfs_ops = {
553 .owner = THIS_MODULE,
554 .open = pwm_seq_open,
555 .read = seq_read,
556 .llseek = seq_lseek,
557 .release = seq_release,
558};
559
560static int __init pwm_debugfs_init(void)
561{
562 debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
563 &pwm_debugfs_ops);
564
565 return 0;
566}
567
568subsys_initcall(pwm_debugfs_init);
569#endif /* CONFIG_DEBUG_FS */