Commit | Line | Data |
---|---|---|
42796d37 | 1 | /* |
2 | * linux/drivers/video/backlight/pwm_bl.c | |
3 | * | |
4 | * simple PWM based backlight control, board code has to setup | |
5 | * 1) pin configuration so PWM waveforms can output | |
b8cdd877 | 6 | * 2) platform_data being correctly configured |
42796d37 | 7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
12 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/platform_device.h> | |
17 | #include <linux/fb.h> | |
18 | #include <linux/backlight.h> | |
19 | #include <linux/err.h> | |
20 | #include <linux/pwm.h> | |
21 | #include <linux/pwm_backlight.h> | |
22 | ||
23 | struct pwm_bl_data { | |
24 | struct pwm_device *pwm; | |
cfc3899f | 25 | struct device *dev; |
42796d37 | 26 | unsigned int period; |
cfc3899f BD |
27 | int (*notify)(struct device *, |
28 | int brightness); | |
42796d37 | 29 | }; |
30 | ||
31 | static int pwm_backlight_update_status(struct backlight_device *bl) | |
32 | { | |
33 | struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); | |
34 | int brightness = bl->props.brightness; | |
35 | int max = bl->props.max_brightness; | |
36 | ||
37 | if (bl->props.power != FB_BLANK_UNBLANK) | |
38 | brightness = 0; | |
39 | ||
40 | if (bl->props.fb_blank != FB_BLANK_UNBLANK) | |
41 | brightness = 0; | |
42 | ||
3b73125a | 43 | if (pb->notify) |
cfc3899f | 44 | brightness = pb->notify(pb->dev, brightness); |
3b73125a | 45 | |
42796d37 | 46 | if (brightness == 0) { |
47 | pwm_config(pb->pwm, 0, pb->period); | |
48 | pwm_disable(pb->pwm); | |
49 | } else { | |
50 | pwm_config(pb->pwm, brightness * pb->period / max, pb->period); | |
51 | pwm_enable(pb->pwm); | |
52 | } | |
53 | return 0; | |
54 | } | |
55 | ||
56 | static int pwm_backlight_get_brightness(struct backlight_device *bl) | |
57 | { | |
58 | return bl->props.brightness; | |
59 | } | |
60 | ||
9905a43b | 61 | static const struct backlight_ops pwm_backlight_ops = { |
42796d37 | 62 | .update_status = pwm_backlight_update_status, |
63 | .get_brightness = pwm_backlight_get_brightness, | |
64 | }; | |
65 | ||
66 | static int pwm_backlight_probe(struct platform_device *pdev) | |
67 | { | |
a19a6ee6 | 68 | struct backlight_properties props; |
42796d37 | 69 | struct platform_pwm_backlight_data *data = pdev->dev.platform_data; |
70 | struct backlight_device *bl; | |
71 | struct pwm_bl_data *pb; | |
3b73125a | 72 | int ret; |
42796d37 | 73 | |
14563a4e BD |
74 | if (!data) { |
75 | dev_err(&pdev->dev, "failed to find platform data\n"); | |
42796d37 | 76 | return -EINVAL; |
14563a4e | 77 | } |
42796d37 | 78 | |
3b73125a PZ |
79 | if (data->init) { |
80 | ret = data->init(&pdev->dev); | |
81 | if (ret < 0) | |
82 | return ret; | |
83 | } | |
84 | ||
42796d37 | 85 | pb = kzalloc(sizeof(*pb), GFP_KERNEL); |
3b73125a | 86 | if (!pb) { |
14563a4e | 87 | dev_err(&pdev->dev, "no memory for state\n"); |
3b73125a PZ |
88 | ret = -ENOMEM; |
89 | goto err_alloc; | |
90 | } | |
42796d37 | 91 | |
92 | pb->period = data->pwm_period_ns; | |
3b73125a | 93 | pb->notify = data->notify; |
cfc3899f | 94 | pb->dev = &pdev->dev; |
42796d37 | 95 | |
96 | pb->pwm = pwm_request(data->pwm_id, "backlight"); | |
43bda1a6 | 97 | if (IS_ERR(pb->pwm)) { |
42796d37 | 98 | dev_err(&pdev->dev, "unable to request PWM for backlight\n"); |
43bda1a6 | 99 | ret = PTR_ERR(pb->pwm); |
3b73125a | 100 | goto err_pwm; |
14563a4e BD |
101 | } else |
102 | dev_dbg(&pdev->dev, "got pwm for backlight\n"); | |
42796d37 | 103 | |
a19a6ee6 MG |
104 | memset(&props, 0, sizeof(struct backlight_properties)); |
105 | props.max_brightness = data->max_brightness; | |
106 | bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb, | |
107 | &pwm_backlight_ops, &props); | |
42796d37 | 108 | if (IS_ERR(bl)) { |
109 | dev_err(&pdev->dev, "failed to register backlight\n"); | |
3b73125a PZ |
110 | ret = PTR_ERR(bl); |
111 | goto err_bl; | |
42796d37 | 112 | } |
113 | ||
42796d37 | 114 | bl->props.brightness = data->dft_brightness; |
115 | backlight_update_status(bl); | |
116 | ||
117 | platform_set_drvdata(pdev, bl); | |
118 | return 0; | |
3b73125a PZ |
119 | |
120 | err_bl: | |
121 | pwm_free(pb->pwm); | |
122 | err_pwm: | |
123 | kfree(pb); | |
124 | err_alloc: | |
125 | if (data->exit) | |
126 | data->exit(&pdev->dev); | |
127 | return ret; | |
42796d37 | 128 | } |
129 | ||
130 | static int pwm_backlight_remove(struct platform_device *pdev) | |
131 | { | |
3b73125a | 132 | struct platform_pwm_backlight_data *data = pdev->dev.platform_data; |
42796d37 | 133 | struct backlight_device *bl = platform_get_drvdata(pdev); |
134 | struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); | |
135 | ||
136 | backlight_device_unregister(bl); | |
137 | pwm_config(pb->pwm, 0, pb->period); | |
138 | pwm_disable(pb->pwm); | |
139 | pwm_free(pb->pwm); | |
140 | kfree(pb); | |
3b73125a PZ |
141 | if (data->exit) |
142 | data->exit(&pdev->dev); | |
42796d37 | 143 | return 0; |
144 | } | |
145 | ||
146 | #ifdef CONFIG_PM | |
147 | static int pwm_backlight_suspend(struct platform_device *pdev, | |
148 | pm_message_t state) | |
149 | { | |
150 | struct backlight_device *bl = platform_get_drvdata(pdev); | |
151 | struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); | |
152 | ||
82e8b542 | 153 | if (pb->notify) |
cfc3899f | 154 | pb->notify(pb->dev, 0); |
42796d37 | 155 | pwm_config(pb->pwm, 0, pb->period); |
156 | pwm_disable(pb->pwm); | |
157 | return 0; | |
158 | } | |
159 | ||
160 | static int pwm_backlight_resume(struct platform_device *pdev) | |
161 | { | |
162 | struct backlight_device *bl = platform_get_drvdata(pdev); | |
163 | ||
164 | backlight_update_status(bl); | |
165 | return 0; | |
166 | } | |
167 | #else | |
168 | #define pwm_backlight_suspend NULL | |
169 | #define pwm_backlight_resume NULL | |
170 | #endif | |
171 | ||
172 | static struct platform_driver pwm_backlight_driver = { | |
173 | .driver = { | |
174 | .name = "pwm-backlight", | |
175 | .owner = THIS_MODULE, | |
176 | }, | |
177 | .probe = pwm_backlight_probe, | |
178 | .remove = pwm_backlight_remove, | |
179 | .suspend = pwm_backlight_suspend, | |
180 | .resume = pwm_backlight_resume, | |
181 | }; | |
182 | ||
183 | static int __init pwm_backlight_init(void) | |
184 | { | |
185 | return platform_driver_register(&pwm_backlight_driver); | |
186 | } | |
187 | module_init(pwm_backlight_init); | |
188 | ||
189 | static void __exit pwm_backlight_exit(void) | |
190 | { | |
191 | platform_driver_unregister(&pwm_backlight_driver); | |
192 | } | |
193 | module_exit(pwm_backlight_exit); | |
194 | ||
195 | MODULE_DESCRIPTION("PWM based Backlight Driver"); | |
196 | MODULE_LICENSE("GPL"); | |
8cd68198 BD |
197 | MODULE_ALIAS("platform:pwm-backlight"); |
198 |