Commit | Line | Data |
---|---|---|
41c42ff5 LF |
1 | /* |
2 | * linux/drivers/leds-pwm.c | |
3 | * | |
4 | * simple PWM based LED control | |
5 | * | |
6 | * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de) | |
7 | * | |
8 | * based on leds-gpio.c by Raphael Assenat <raph@8d.com> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
13 | */ | |
14 | ||
15 | #include <linux/module.h> | |
16 | #include <linux/kernel.h> | |
17 | #include <linux/init.h> | |
18 | #include <linux/platform_device.h> | |
19 | #include <linux/fb.h> | |
20 | #include <linux/leds.h> | |
21 | #include <linux/err.h> | |
22 | #include <linux/pwm.h> | |
23 | #include <linux/leds_pwm.h> | |
5a0e3ad6 | 24 | #include <linux/slab.h> |
41c42ff5 LF |
25 | |
26 | struct led_pwm_data { | |
27 | struct led_classdev cdev; | |
28 | struct pwm_device *pwm; | |
dcba9105 | 29 | unsigned int active_low; |
41c42ff5 | 30 | unsigned int period; |
41c42ff5 LF |
31 | }; |
32 | ||
0f86815a PU |
33 | struct led_pwm_priv { |
34 | int num_leds; | |
35 | struct led_pwm_data leds[0]; | |
36 | }; | |
37 | ||
41c42ff5 LF |
38 | static void led_pwm_set(struct led_classdev *led_cdev, |
39 | enum led_brightness brightness) | |
40 | { | |
41 | struct led_pwm_data *led_dat = | |
42 | container_of(led_cdev, struct led_pwm_data, cdev); | |
e4590620 | 43 | unsigned int max = led_dat->cdev.max_brightness; |
41c42ff5 LF |
44 | unsigned int period = led_dat->period; |
45 | ||
46 | if (brightness == 0) { | |
47 | pwm_config(led_dat->pwm, 0, period); | |
48 | pwm_disable(led_dat->pwm); | |
49 | } else { | |
50 | pwm_config(led_dat->pwm, brightness * period / max, period); | |
51 | pwm_enable(led_dat->pwm); | |
52 | } | |
53 | } | |
54 | ||
0f86815a PU |
55 | static inline size_t sizeof_pwm_leds_priv(int num_leds) |
56 | { | |
57 | return sizeof(struct led_pwm_priv) + | |
58 | (sizeof(struct led_pwm_data) * num_leds); | |
59 | } | |
60 | ||
41c42ff5 LF |
61 | static int led_pwm_probe(struct platform_device *pdev) |
62 | { | |
63 | struct led_pwm_platform_data *pdata = pdev->dev.platform_data; | |
0f86815a | 64 | struct led_pwm_priv *priv; |
41c42ff5 LF |
65 | int i, ret = 0; |
66 | ||
67 | if (!pdata) | |
68 | return -EBUSY; | |
69 | ||
0f86815a PU |
70 | priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(pdata->num_leds), |
71 | GFP_KERNEL); | |
72 | if (!priv) | |
41c42ff5 LF |
73 | return -ENOMEM; |
74 | ||
75 | for (i = 0; i < pdata->num_leds; i++) { | |
0f86815a PU |
76 | struct led_pwm *cur_led = &pdata->leds[i]; |
77 | struct led_pwm_data *led_dat = &priv->leds[i]; | |
41c42ff5 | 78 | |
9ea6cdac | 79 | led_dat->pwm = devm_pwm_get(&pdev->dev, cur_led->name); |
41c42ff5 | 80 | if (IS_ERR(led_dat->pwm)) { |
d8cc667b | 81 | ret = PTR_ERR(led_dat->pwm); |
9ea6cdac PU |
82 | dev_err(&pdev->dev, "unable to request PWM for %s\n", |
83 | cur_led->name); | |
41c42ff5 LF |
84 | goto err; |
85 | } | |
86 | ||
87 | led_dat->cdev.name = cur_led->name; | |
88 | led_dat->cdev.default_trigger = cur_led->default_trigger; | |
89 | led_dat->active_low = cur_led->active_low; | |
41c42ff5 LF |
90 | led_dat->period = cur_led->pwm_period_ns; |
91 | led_dat->cdev.brightness_set = led_pwm_set; | |
92 | led_dat->cdev.brightness = LED_OFF; | |
e4590620 | 93 | led_dat->cdev.max_brightness = cur_led->max_brightness; |
41c42ff5 LF |
94 | led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME; |
95 | ||
96 | ret = led_classdev_register(&pdev->dev, &led_dat->cdev); | |
9ea6cdac | 97 | if (ret < 0) |
41c42ff5 | 98 | goto err; |
41c42ff5 | 99 | } |
0f86815a | 100 | priv->num_leds = pdata->num_leds; |
41c42ff5 | 101 | |
0f86815a | 102 | platform_set_drvdata(pdev, priv); |
41c42ff5 LF |
103 | |
104 | return 0; | |
105 | ||
106 | err: | |
8a66a579 PU |
107 | while (i--) |
108 | led_classdev_unregister(&priv->leds[i].cdev); | |
41c42ff5 | 109 | |
41c42ff5 LF |
110 | return ret; |
111 | } | |
112 | ||
678e8a6b | 113 | static int led_pwm_remove(struct platform_device *pdev) |
41c42ff5 | 114 | { |
0f86815a | 115 | struct led_pwm_priv *priv = platform_get_drvdata(pdev); |
41c42ff5 | 116 | int i; |
41c42ff5 | 117 | |
0f86815a PU |
118 | for (i = 0; i < priv->num_leds; i++) |
119 | led_classdev_unregister(&priv->leds[i].cdev); | |
41c42ff5 | 120 | |
41c42ff5 LF |
121 | return 0; |
122 | } | |
123 | ||
124 | static struct platform_driver led_pwm_driver = { | |
125 | .probe = led_pwm_probe, | |
df07cf81 | 126 | .remove = led_pwm_remove, |
41c42ff5 LF |
127 | .driver = { |
128 | .name = "leds_pwm", | |
129 | .owner = THIS_MODULE, | |
130 | }, | |
131 | }; | |
132 | ||
892a8843 | 133 | module_platform_driver(led_pwm_driver); |
41c42ff5 LF |
134 | |
135 | MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>"); | |
136 | MODULE_DESCRIPTION("PWM LED driver for PXA"); | |
137 | MODULE_LICENSE("GPL"); | |
138 | MODULE_ALIAS("platform:leds-pwm"); |