2 * linux/drivers/video/backlight/pwm_bl.c
4 * simple PWM based backlight control, board code has to setup
5 * 1) pin configuration so PWM waveforms can output
6 * 2) platform_data casts to the PWM id (0/1/2/3 on PXA)
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.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/platform_device.h>
18 #include <linux/backlight.h>
19 #include <linux/err.h>
20 #include <linux/pwm.h>
21 #include <linux/pwm_backlight.h>
24 struct pwm_device *pwm;
26 int (*notify)(int brightness);
29 static int pwm_backlight_update_status(struct backlight_device *bl)
31 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
32 int brightness = bl->props.brightness;
33 int max = bl->props.max_brightness;
35 if (bl->props.power != FB_BLANK_UNBLANK)
38 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
42 brightness = pb->notify(brightness);
44 if (brightness == 0) {
45 pwm_config(pb->pwm, 0, pb->period);
48 pwm_config(pb->pwm, brightness * pb->period / max, pb->period);
54 static int pwm_backlight_get_brightness(struct backlight_device *bl)
56 return bl->props.brightness;
59 static struct backlight_ops pwm_backlight_ops = {
60 .update_status = pwm_backlight_update_status,
61 .get_brightness = pwm_backlight_get_brightness,
64 static int pwm_backlight_probe(struct platform_device *pdev)
66 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
67 struct backlight_device *bl;
68 struct pwm_bl_data *pb;
75 ret = data->init(&pdev->dev);
80 pb = kzalloc(sizeof(*pb), GFP_KERNEL);
86 pb->period = data->pwm_period_ns;
87 pb->notify = data->notify;
89 pb->pwm = pwm_request(data->pwm_id, "backlight");
90 if (IS_ERR(pb->pwm)) {
91 dev_err(&pdev->dev, "unable to request PWM for backlight\n");
92 ret = PTR_ERR(pb->pwm);
96 bl = backlight_device_register(pdev->name, &pdev->dev,
97 pb, &pwm_backlight_ops);
99 dev_err(&pdev->dev, "failed to register backlight\n");
104 bl->props.max_brightness = data->max_brightness;
105 bl->props.brightness = data->dft_brightness;
106 backlight_update_status(bl);
108 platform_set_drvdata(pdev, bl);
117 data->exit(&pdev->dev);
121 static int pwm_backlight_remove(struct platform_device *pdev)
123 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
124 struct backlight_device *bl = platform_get_drvdata(pdev);
125 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
127 backlight_device_unregister(bl);
128 pwm_config(pb->pwm, 0, pb->period);
129 pwm_disable(pb->pwm);
133 data->exit(&pdev->dev);
138 static int pwm_backlight_suspend(struct platform_device *pdev,
141 struct backlight_device *bl = platform_get_drvdata(pdev);
142 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
144 pwm_config(pb->pwm, 0, pb->period);
145 pwm_disable(pb->pwm);
149 static int pwm_backlight_resume(struct platform_device *pdev)
151 struct backlight_device *bl = platform_get_drvdata(pdev);
153 backlight_update_status(bl);
157 #define pwm_backlight_suspend NULL
158 #define pwm_backlight_resume NULL
161 static struct platform_driver pwm_backlight_driver = {
163 .name = "pwm-backlight",
164 .owner = THIS_MODULE,
166 .probe = pwm_backlight_probe,
167 .remove = pwm_backlight_remove,
168 .suspend = pwm_backlight_suspend,
169 .resume = pwm_backlight_resume,
172 static int __init pwm_backlight_init(void)
174 return platform_driver_register(&pwm_backlight_driver);
176 module_init(pwm_backlight_init);
178 static void __exit pwm_backlight_exit(void)
180 platform_driver_unregister(&pwm_backlight_driver);
182 module_exit(pwm_backlight_exit);
184 MODULE_DESCRIPTION("PWM based Backlight Driver");
185 MODULE_LICENSE("GPL");