PWM: ecap: Clean the access method for PWMSS config space.
[sitara-epos/sitara-epos-kernel.git] / drivers / pwm / pwm.c
1 /*
2  * PWM API implementation
3  *
4  * Copyright (C) 2011 Bill Gatliff <bgat@billgatliff.com>
5  * Copyright (C) 2011 Arun Murthy <arun.murthy@stericsson.com>
6  *
7  * This program is free software; you may redistribute and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  */
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/device.h>
27 #include <linux/fs.h>
28 #include <linux/completion.h>
29 #include <linux/workqueue.h>
30 #include <linux/list.h>
31 #include <linux/sched.h>
32 #include <linux/platform_device.h>
33 #include <linux/cpufreq.h>
34 #include <linux/pwm/pwm.h>
36 static const char *REQUEST_SYSFS = "sysfs";
37 static LIST_HEAD(pwm_device_list);
38 static DEFINE_MUTEX(device_list_mutex);
39 static struct class pwm_class;
40 static struct workqueue_struct *pwm_handler_workqueue;
42 static int pwm_match_name(struct device *dev, void *name)
43 {
44         return !strcmp(name, dev_name(dev));
45 }
47 static struct pwm_device *__pwm_request(struct pwm_device *p, const char *label)
48 {
49         int ret;
51         ret = test_and_set_bit(FLAG_REQUESTED, &p->flags);
52         if (ret) {
53                 p = ERR_PTR(-EBUSY);
54                 goto done;
55         }
57         p->label = label;
58         p->pid = current->pid;
60         if (p->ops->request) {
61                 ret = p->ops->request(p);
62                 if (ret) {
63                         p = ERR_PTR(ret);
64                         clear_bit(FLAG_REQUESTED, &p->flags);
65                         goto done;
66                 }
67         }
69 done:
70         return p;
71 }
73 static struct pwm_device *__pwm_request_byname(const char *name,
74                                                const char *label)
75 {
76         struct device *d;
77         struct pwm_device *p;
79         d = class_find_device(&pwm_class, NULL, (char *)name, pwm_match_name);
80         if (!d) {
81                 p = ERR_PTR(-EINVAL);
82                 goto done;
83         }
84         if (IS_ERR(d)) {
85                 p = (struct pwm_device *)d;
86                 goto done;
87         }
89         p = __pwm_request(dev_get_drvdata(d), label);
91 done:
92         return p;
93 }
95 struct pwm_device *pwm_request_byname(const char *name, const char *label)
96 {
97         struct pwm_device *p;
99         mutex_lock(&device_list_mutex);
100         p = __pwm_request_byname(name, label);
101         mutex_unlock(&device_list_mutex);
102         return p;
104 EXPORT_SYMBOL(pwm_request_byname);
106 struct pwm_device *pwm_request(const char *bus_id, int id, const char *label)
108         char name[256];
109         int ret;
111         if (id == -1)
112                 ret = scnprintf(name, sizeof name, "%s", bus_id);
113         else
114                 ret = scnprintf(name, sizeof name, "%s:%d", bus_id, id);
115         if (ret <= 0 || ret >= sizeof name)
116                 return ERR_PTR(-EINVAL);
118         return pwm_request_byname(name, label);
120 EXPORT_SYMBOL(pwm_request);
122 void pwm_release(struct pwm_device *p)
124         mutex_lock(&device_list_mutex);
126         if (!test_and_clear_bit(FLAG_REQUESTED, &p->flags)) {
127                 pr_debug("%s pwm device is not requested!\n",
128                                 dev_name(p->dev));
129                 goto done;
130         }
132         pwm_stop(p);
133         pwm_unsynchronize(p, NULL);
134         pwm_set_handler(p, NULL, NULL);
136         p->label = NULL;
137         p->pid = -1;
139         if (p->ops->release)
140                 p->ops->release(p);
141 done:
142         mutex_unlock(&device_list_mutex);
144 EXPORT_SYMBOL(pwm_release);
146 unsigned long pwm_ns_to_ticks(struct pwm_device *p, unsigned long nsecs)
148         unsigned long long ticks;
150         ticks = nsecs;
151         ticks *= p->tick_hz;
152         do_div(ticks, 1000000000);
153         return ticks;
155 EXPORT_SYMBOL(pwm_ns_to_ticks);
157 unsigned long pwm_ticks_to_ns(struct pwm_device *p, unsigned long ticks)
159         unsigned long long ns;
161         if (!p->tick_hz) {
162                 pr_debug("%s: frequency is zero\n", dev_name(p->dev));
163                 return 0;
164         }
166         ns = ticks;
167         ns *= 1000000000UL;
168         do_div(ns, p->tick_hz);
169         return ns;
171 EXPORT_SYMBOL(pwm_ticks_to_ns);
173 static void pwm_config_ns_to_ticks(struct pwm_device *p, struct pwm_config *c)
175         if (test_bit(PWM_CONFIG_PERIOD_NS, &c->config_mask)) {
176                 c->period_ticks = pwm_ns_to_ticks(p, c->period_ns);
177                 clear_bit(PWM_CONFIG_PERIOD_NS, &c->config_mask);
178                 set_bit(PWM_CONFIG_PERIOD_TICKS, &c->config_mask);
179         }
181         if (test_bit(PWM_CONFIG_DUTY_NS, &c->config_mask)) {
182                 c->duty_ticks = pwm_ns_to_ticks(p, c->duty_ns);
183                 clear_bit(PWM_CONFIG_DUTY_NS, &c->config_mask);
184                 set_bit(PWM_CONFIG_DUTY_TICKS, &c->config_mask);
185         }
188 static void pwm_config_percent_to_ticks(struct pwm_device *p,
189                                         struct pwm_config *c)
191         if (test_bit(PWM_CONFIG_DUTY_PERCENT, &c->config_mask)) {
192                 if (test_bit(PWM_CONFIG_PERIOD_TICKS, &c->config_mask))
193                         c->duty_ticks = c->period_ticks;
194                 else
195                         c->duty_ticks = p->period_ticks;
197                 c->duty_ticks *= c->duty_percent;
198                 c->duty_ticks /= 100;
199                 clear_bit(PWM_CONFIG_DUTY_PERCENT, &c->config_mask);
200                 set_bit(PWM_CONFIG_DUTY_TICKS, &c->config_mask);
201         }
204 int pwm_config_nosleep(struct pwm_device *p, struct pwm_config *c)
206         if (!p->ops->config_nosleep)
207                 return -EINVAL;
209         pwm_config_ns_to_ticks(p, c);
210         pwm_config_percent_to_ticks(p, c);
212         return p->ops->config_nosleep(p, c);
214 EXPORT_SYMBOL(pwm_config_nosleep);
216 int pwm_config(struct pwm_device *p, struct pwm_config *c)
218         int ret = 0;
220         pwm_config_ns_to_ticks(p, c);
221         pwm_config_percent_to_ticks(p, c);
223         switch (c->config_mask & (BIT(PWM_CONFIG_PERIOD_TICKS)
224                                   | BIT(PWM_CONFIG_DUTY_TICKS))) {
225         case BIT(PWM_CONFIG_PERIOD_TICKS):
226                 if (p->duty_ticks > c->period_ticks) {
227                         ret = -EINVAL;
228                         goto err;
229                 }
230                 break;
231         case BIT(PWM_CONFIG_DUTY_TICKS):
232                 if (p->period_ticks < c->duty_ticks) {
233                         ret = -EINVAL;
234                         goto err;
235                 }
236                 break;
237         case BIT(PWM_CONFIG_DUTY_TICKS) | BIT(PWM_CONFIG_PERIOD_TICKS):
238                 if (c->duty_ticks > c->period_ticks) {
239                         ret = -EINVAL;
240                         goto err;
241                 }
242                 break;
243         default:
244                 break;
245         }
247 err:
248         dev_dbg(p->dev, "%s: config_mask %lu period_ticks %lu duty_ticks %lu"
249                 " polarity %d duty_ns %lu period_ns %lu duty_percent %d\n",
250                 __func__, c->config_mask, c->period_ticks, c->duty_ticks,
251                 c->polarity, c->duty_ns, c->period_ns, c->duty_percent);
253         if (ret)
254                 return ret;
255         spin_lock(&p->pwm_lock);
256         ret = p->ops->config(p, c);
257         spin_unlock(&p->pwm_lock);
258         return ret;
260 EXPORT_SYMBOL(pwm_config);
262 int pwm_set_period_ns(struct pwm_device *p, unsigned long period_ns)
264         struct pwm_config c = {
265                 .config_mask = BIT(PWM_CONFIG_PERIOD_TICKS),
266                 .period_ticks = pwm_ns_to_ticks(p, period_ns),
267         };
269         spin_lock(&p->pwm_lock);
270         p->period_ns = period_ns;
271         spin_unlock(&p->pwm_lock);
272         return pwm_config(p, &c);
274 EXPORT_SYMBOL(pwm_set_period_ns);
276 unsigned long pwm_get_period_ns(struct pwm_device *p)
278         return pwm_ticks_to_ns(p, p->period_ticks);
280 EXPORT_SYMBOL(pwm_get_period_ns);
282 int pwm_set_frequency(struct pwm_device *p, unsigned long freq)
284         struct pwm_config c;
286         if (!freq)
287                 return -EINVAL;
289         c.config_mask = BIT(PWM_CONFIG_PERIOD_TICKS),
290         c.period_ticks = pwm_ns_to_ticks(p, (NSEC_PER_SEC / freq)),
291         spin_lock(&p->pwm_lock);
292         p->period_ns = NSEC_PER_SEC / freq;
293         spin_unlock(&p->pwm_lock);
294         return pwm_config(p, &c);
296 EXPORT_SYMBOL(pwm_set_frequency);
298 unsigned long pwm_get_frequency(struct pwm_device *p)
300         unsigned long period_ns;
302          period_ns = pwm_ticks_to_ns(p, p->period_ticks);
304         if (!period_ns) {
305                 pr_debug("%s: frequency is zero\n", dev_name(p->dev));
306                 return 0;
307         }
309         return  NSEC_PER_SEC / period_ns;
311 EXPORT_SYMBOL(pwm_get_frequency);
313 int pwm_set_period_ticks(struct pwm_device *p,
314                                         unsigned long ticks)
316         struct pwm_config c = {
317                 .config_mask = BIT(PWM_CONFIG_PERIOD_TICKS),
318                 .period_ticks = ticks,
319         };
321         spin_lock(&p->pwm_lock);
322         p->period_ns = pwm_ticks_to_ns(p, ticks);
323         spin_unlock(&p->pwm_lock);
324         return pwm_config(p, &c);
326 EXPORT_SYMBOL(pwm_set_period_ticks);
328 int pwm_set_duty_ns(struct pwm_device *p, unsigned long duty_ns)
330         struct pwm_config c = {
331                 .config_mask = BIT(PWM_CONFIG_DUTY_TICKS),
332                 .duty_ticks = pwm_ns_to_ticks(p, duty_ns),
333         };
334         spin_lock(&p->pwm_lock);
335         p->duty_ns = duty_ns;
336         spin_unlock(&p->pwm_lock);
337         return pwm_config(p, &c);
339 EXPORT_SYMBOL(pwm_set_duty_ns);
341 unsigned long pwm_get_duty_ns(struct pwm_device *p)
343         return pwm_ticks_to_ns(p, p->duty_ticks);
345 EXPORT_SYMBOL(pwm_get_duty_ns);
347 int pwm_set_duty_percent(struct pwm_device *p, int percent)
349         struct pwm_config c = {
350                 .config_mask = BIT(PWM_CONFIG_DUTY_PERCENT),
351                 .duty_percent = percent,
352         };
354         spin_lock(&p->pwm_lock);
355         p->duty_ns = p->period_ns * percent;
356         p->duty_ns /= 100;
357         spin_unlock(&p->pwm_lock);
358         return pwm_config(p, &c);
360 EXPORT_SYMBOL(pwm_set_duty_percent);
362 unsigned long pwm_get_duty_percent(struct pwm_device *p)
364         unsigned long long duty_percent;
366         if (!p->period_ns) {
367                 pr_debug("%s: frequency is zero\n", dev_name(p->dev));
368                 return 0;
369         }
371         duty_percent = pwm_ticks_to_ns(p, p->duty_ticks);
372         duty_percent *= 100;
373         do_div(duty_percent, p->period_ns);
374         return duty_percent;
376 EXPORT_SYMBOL(pwm_get_duty_percent);
378 int pwm_set_duty_ticks(struct pwm_device *p,
379                                         unsigned long ticks)
381         struct pwm_config c = {
382                 .config_mask = BIT(PWM_CONFIG_DUTY_TICKS),
383                 .duty_ticks = ticks,
384         };
386         spin_lock(&p->pwm_lock);
387         p->duty_ns = pwm_ticks_to_ns(p, ticks);
388         spin_unlock(&p->pwm_lock);
389         return pwm_config(p, &c);
391 EXPORT_SYMBOL(pwm_set_duty_ticks);
393 int pwm_set_polarity(struct pwm_device *p, int active_high)
395         struct pwm_config c = {
396                 .config_mask = BIT(PWM_CONFIG_POLARITY),
397                 .polarity = active_high,
398         };
399         return pwm_config(p, &c);
401 EXPORT_SYMBOL(pwm_set_polarity);
403 int pwm_start(struct pwm_device *p)
405         struct pwm_config c = {
406                 .config_mask = BIT(PWM_CONFIG_START),
407         };
408         return pwm_config(p, &c);
410 EXPORT_SYMBOL(pwm_start);
412 int pwm_stop(struct pwm_device *p)
414         struct pwm_config c = {
415                 .config_mask = BIT(PWM_CONFIG_STOP),
416         };
417         return pwm_config(p, &c);
419 EXPORT_SYMBOL(pwm_stop);
421 int pwm_synchronize(struct pwm_device *p, struct pwm_device *to_p)
423         if (!p->ops->synchronize)
424                 return -EINVAL;
426         return p->ops->synchronize(p, to_p);
428 EXPORT_SYMBOL(pwm_synchronize);
430 int pwm_unsynchronize(struct pwm_device *p, struct pwm_device *from_p)
432         if (!p->ops->unsynchronize)
433                 return -EINVAL;
435         return p->ops->unsynchronize(p, from_p);
437 EXPORT_SYMBOL(pwm_unsynchronize);
439 static void pwm_handler(struct work_struct *w)
441         struct pwm_device *p = container_of(w, struct pwm_device,
442                                             handler_work);
443         if (p->handler && p->handler(p, p->handler_data))
444                 pwm_stop(p);
447 static void __pwm_callback(struct pwm_device *p)
449         queue_work(pwm_handler_workqueue, &p->handler_work);
452 int pwm_set_handler(struct pwm_device *p, pwm_handler_t handler, void *data)
454         if (p->ops->set_callback) {
455                 p->handler_data = data;
456                 p->handler = handler;
457                 INIT_WORK(&p->handler_work, pwm_handler);
458                 return p->ops->set_callback(p, handler ? __pwm_callback : NULL);
459         }
460         return -EINVAL;
462 EXPORT_SYMBOL(pwm_set_handler);
464 static ssize_t pwm_run_show(struct device *dev,
465                             struct device_attribute *attr,
466                             char *buf)
468         struct pwm_device *p = dev_get_drvdata(dev);
469         return sprintf(buf, "%d\n", pwm_is_running(p));
472 static ssize_t pwm_run_store(struct device *dev,
473                              struct device_attribute *attr,
474                              const char *buf, size_t len)
476         struct pwm_device *p = dev_get_drvdata(dev);
477         int ret;
479         if (sysfs_streq(buf, "1"))
480                 ret = pwm_start(p);
481         else if (sysfs_streq(buf, "0"))
482                 ret = pwm_stop(p);
483         else
484                 ret = -EINVAL;
486         if (ret < 0)
487                 return ret;
488         return len;
490 static DEVICE_ATTR(run, S_IRUGO | S_IWUSR, pwm_run_show, pwm_run_store);
492 static ssize_t pwm_tick_hz_show(struct device *dev,
493                                 struct device_attribute *attr,
494                                 char *buf)
496         struct pwm_device *p = dev_get_drvdata(dev);
497         return sprintf(buf, "%lu\n", p->tick_hz);
499 static DEVICE_ATTR(tick_hz, S_IRUGO, pwm_tick_hz_show, NULL);
501 static ssize_t pwm_duty_ns_show(struct device *dev,
502                                 struct device_attribute *attr,
503                                 char *buf)
505         struct pwm_device *p = dev_get_drvdata(dev);
506         return sprintf(buf, "%lu\n", pwm_get_duty_ns(p));
509 static ssize_t pwm_duty_ns_store(struct device *dev,
510                                  struct device_attribute *attr,
511                                  const char *buf, size_t len)
513         unsigned long duty_ns;
514         struct pwm_device *p = dev_get_drvdata(dev);
515         int ret;
517         if (!kstrtoul(buf, 10, &duty_ns)) {
518                 ret = pwm_set_duty_ns(p, duty_ns);
520                 if (ret < 0)
521                         return ret;
522         }
524         return len;
526 static DEVICE_ATTR(duty_ns, S_IRUGO | S_IWUSR, pwm_duty_ns_show,
527                pwm_duty_ns_store);
529 static ssize_t pwm_duty_percent_show(struct device *dev,
530                                 struct device_attribute *attr,
531                                 char *buf)
533         struct pwm_device *p = dev_get_drvdata(dev);
534         return sprintf(buf, "%lu\n", pwm_get_duty_percent(p));
537 static ssize_t pwm_duty_percent_store(struct device *dev,
538                                  struct device_attribute *attr,
539                                  const char *buf,
540                                  size_t len)
542         unsigned long duty_ns;
543         struct pwm_device *p = dev_get_drvdata(dev);
544         int ret;
546         if (!kstrtoul(buf, 10, &duty_ns)) {
547                 ret = pwm_set_duty_percent(p, duty_ns);
549                 if (ret < 0)
550                         return ret;
551         }
553         return len;
556 static DEVICE_ATTR(duty_percent, S_IRUGO | S_IWUSR, pwm_duty_percent_show,
557                pwm_duty_percent_store);
559 static ssize_t pwm_period_ns_show(struct device *dev,
560                                   struct device_attribute *attr,
561                                   char *buf)
563         struct pwm_device *p = dev_get_drvdata(dev);
564         return sprintf(buf, "%lu\n", pwm_get_period_ns(p));
567 static ssize_t pwm_period_ns_store(struct device *dev,
568                                    struct device_attribute *attr,
569                                    const char *buf, size_t len)
571         unsigned long period_ns;
572         struct pwm_device *p = dev_get_drvdata(dev);
573         int ret;
575         if (!kstrtoul(buf, 10, &period_ns)) {
576                 ret = pwm_set_period_ns(p, period_ns);
578                 if (ret < 0)
579                         return ret;
580         }
582         return len;
584 static DEVICE_ATTR(period_ns, S_IRUGO | S_IWUSR, pwm_period_ns_show,
585                pwm_period_ns_store);
587 static ssize_t pwm_period_freq_show(struct device *dev,
588                                   struct device_attribute *attr,
589                                   char *buf)
591         struct pwm_device *p = dev_get_drvdata(dev);
592         return sprintf(buf, "%lu\n", pwm_get_frequency(p));
595 static ssize_t pwm_period_freq_store(struct device *dev,
596                                    struct device_attribute *attr,
597                                    const char *buf,
598                                    size_t len)
600         unsigned long freq_hz;
601         int ret;
603         struct pwm_device *p = dev_get_drvdata(dev);
604         if (!kstrtoul(buf, 10, &freq_hz)) {
605                 ret = pwm_set_frequency(p, freq_hz);
607                 if (ret < 0)
608                         return ret;
609         }
610         return len;
613 static DEVICE_ATTR(period_freq, S_IRUGO | S_IWUSR, pwm_period_freq_show,
614                pwm_period_freq_store);
616 static ssize_t pwm_polarity_show(struct device *dev,
617                                  struct device_attribute *attr,
618                                  char *buf)
620         struct pwm_device *p = dev_get_drvdata(dev);
621         return sprintf(buf, "%d\n", p->active_high ? 1 : 0);
624 static ssize_t pwm_polarity_store(struct device *dev,
625                                   struct device_attribute *attr,
626                                   const char *buf, size_t len)
628         unsigned long polarity;
629         struct pwm_device *p = dev_get_drvdata(dev);
630         int ret;
632         if (!kstrtoul(buf, 10, &polarity)) {
633                 ret = pwm_set_polarity(p, polarity);
635                 if (ret < 0)
636                         return ret;
637         }
639         return len;
641 static DEVICE_ATTR(polarity, S_IRUGO | S_IWUSR, pwm_polarity_show,
642                pwm_polarity_store);
644 static ssize_t pwm_request_show(struct device *dev,
645                                 struct device_attribute *attr,
646                                 char *buf)
648         struct pwm_device *p = dev_get_drvdata(dev);
649         int ret;
651         ret = test_bit(FLAG_REQUESTED, &p->flags);
653         if (ret)
654                 return sprintf(buf, "%s requested by %s\n",
655                                 dev_name(p->dev), p->label);
656         else
657                 return sprintf(buf, "%s is free\n",
658                                 dev_name(p->dev));
661 static ssize_t pwm_request_store(struct device *dev,
662                                  struct device_attribute *attr,
663                                  const char *buf, size_t len)
665         struct pwm_device *p = dev_get_drvdata(dev);
666         unsigned long request;
667         struct pwm_device *ret;
669         if (!kstrtoul(buf, 10, &request)) {
670                 if (request) {
671                         mutex_lock(&device_list_mutex);
672                         ret = __pwm_request(p, REQUEST_SYSFS);
673                         mutex_unlock(&device_list_mutex);
675                         if (IS_ERR(ret))
676                                 return PTR_ERR(ret);
677                 } else
678                         pwm_release(p);
679         }
681         return len;
683 static DEVICE_ATTR(request, S_IRUGO | S_IWUSR, pwm_request_show,
684                pwm_request_store);
686 static const struct attribute *pwm_attrs[] = {
687         &dev_attr_tick_hz.attr,
688         &dev_attr_run.attr,
689         &dev_attr_polarity.attr,
690         &dev_attr_duty_ns.attr,
691         &dev_attr_period_ns.attr,
692         &dev_attr_request.attr,
693         &dev_attr_duty_percent.attr,
694         &dev_attr_period_freq.attr,
695         NULL,
696 };
698 static const struct attribute_group pwm_device_attr_group = {
699         .attrs = (struct attribute **) pwm_attrs,
700 };
702 static struct class_attribute pwm_class_attrs[] = {
703         __ATTR_NULL,
704 };
706 static struct class pwm_class = {
707         .name = "pwm",
708         .owner = THIS_MODULE,
710         .class_attrs = pwm_class_attrs,
711 };
713 static int pwm_freq_transition_notifier_cb(struct notifier_block *nb,
714                 unsigned long val, void *data)
716         struct pwm_device *p;
718         p = container_of(nb, struct pwm_device, freq_transition);
720         if (val == CPUFREQ_POSTCHANGE && pwm_is_requested(p))
721                 p->ops->freq_transition_notifier_cb(p);
723         return 0;
726 static inline int pwm_cpufreq_notifier_register(struct pwm_device *p)
728         p->freq_transition.notifier_call = pwm_freq_transition_notifier_cb;
730         return cpufreq_register_notifier(&p->freq_transition,
731                        CPUFREQ_TRANSITION_NOTIFIER);
734 int pwm_register_byname(struct pwm_device *p, struct device *parent,
735                         const char *name)
737         struct device *d;
738         int ret;
740         if (!p->ops || !p->ops->config)
741                 return -EINVAL;
743         mutex_lock(&device_list_mutex);
745         d = class_find_device(&pwm_class, NULL, (char *)name, pwm_match_name);
746         if (d) {
747                 ret = -EEXIST;
748                 goto err_found_device;
749         }
751         p->dev = device_create(&pwm_class, parent, MKDEV(0, 0), NULL, name);
752         if (IS_ERR(p->dev)) {
753                 ret = PTR_ERR(p->dev);
754                 goto err_device_create;
755         }
757         ret = sysfs_create_group(&p->dev->kobj, &pwm_device_attr_group);
758         if (ret)
759                 goto err_create_group;
761         dev_set_drvdata(p->dev, p);
762         p->flags = BIT(FLAG_REGISTERED);
764         ret = pwm_cpufreq_notifier_register(p);
766         if (ret < 0)
767                 printk(KERN_ERR "Failed to add cpufreq notifier\n");
769         spin_lock_init(&p->pwm_lock);
770         goto done;
772 err_create_group:
773         device_unregister(p->dev);
774         p->flags = 0;
776 err_device_create:
777 err_found_device:
778 done:
779         mutex_unlock(&device_list_mutex);
781         return ret;
783 EXPORT_SYMBOL(pwm_register_byname);
785 int pwm_register(struct pwm_device *p, struct device *parent, int id)
787         int ret;
788         char name[256];
790         if (IS_ERR_OR_NULL(parent))
791                 return -EINVAL;
793         if (id == -1)
794                 ret = scnprintf(name, sizeof name, "%s", dev_name(parent));
795         else
796                 ret = scnprintf(name, sizeof name, "%s:%d",
797                                dev_name(parent), id);
798         if (ret <= 0 || ret >= sizeof name)
799                 return -EINVAL;
801         return pwm_register_byname(p, parent, name);
803 EXPORT_SYMBOL(pwm_register);
805 int pwm_unregister(struct pwm_device *p)
807         int ret = 0;
809         mutex_lock(&device_list_mutex);
811         if (pwm_is_running(p) || pwm_is_requested(p)) {
812                 ret = -EBUSY;
813                 goto done;
814         }
816         sysfs_remove_group(&p->dev->kobj, &pwm_device_attr_group);
817         device_unregister(p->dev);
818         p->flags = 0;
820 done:
821         mutex_unlock(&device_list_mutex);
823         return ret;
825 EXPORT_SYMBOL(pwm_unregister);
827 static int __init pwm_init(void)
829         return class_register(&pwm_class);
832 static void __exit pwm_exit(void)
834         class_unregister(&pwm_class);
837 #ifdef MODULE
838 module_init(pwm_init);
839 module_exit(pwm_exit);
840 MODULE_LICENSE("GPL");
841 #else
842 postcore_initcall(pwm_init);
843 #endif