close_port, kpacket_gen kmalloc oom, formard.c wake_sender/skb receive oom handling...
[cor_2_6_31.git] / drivers / video / backlight / pwm_bl.c
blob88716626744391a89cae97a5658fb3cc0ffbd545
1 /*
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 being correctly configured
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>
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>
23 struct pwm_bl_data {
24 struct pwm_device *pwm;
25 unsigned int period;
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)
36 brightness = 0;
38 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
39 brightness = 0;
41 if (pb->notify)
42 brightness = pb->notify(brightness);
44 if (brightness == 0) {
45 pwm_config(pb->pwm, 0, pb->period);
46 pwm_disable(pb->pwm);
47 } else {
48 pwm_config(pb->pwm, brightness * pb->period / max, pb->period);
49 pwm_enable(pb->pwm);
51 return 0;
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;
69 int ret;
71 if (!data) {
72 dev_err(&pdev->dev, "failed to find platform data\n");
73 return -EINVAL;
76 if (data->init) {
77 ret = data->init(&pdev->dev);
78 if (ret < 0)
79 return ret;
82 pb = kzalloc(sizeof(*pb), GFP_KERNEL);
83 if (!pb) {
84 dev_err(&pdev->dev, "no memory for state\n");
85 ret = -ENOMEM;
86 goto err_alloc;
89 pb->period = data->pwm_period_ns;
90 pb->notify = data->notify;
92 pb->pwm = pwm_request(data->pwm_id, "backlight");
93 if (IS_ERR(pb->pwm)) {
94 dev_err(&pdev->dev, "unable to request PWM for backlight\n");
95 ret = PTR_ERR(pb->pwm);
96 goto err_pwm;
97 } else
98 dev_dbg(&pdev->dev, "got pwm for backlight\n");
100 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev,
101 pb, &pwm_backlight_ops);
102 if (IS_ERR(bl)) {
103 dev_err(&pdev->dev, "failed to register backlight\n");
104 ret = PTR_ERR(bl);
105 goto err_bl;
108 bl->props.max_brightness = data->max_brightness;
109 bl->props.brightness = data->dft_brightness;
110 backlight_update_status(bl);
112 platform_set_drvdata(pdev, bl);
113 return 0;
115 err_bl:
116 pwm_free(pb->pwm);
117 err_pwm:
118 kfree(pb);
119 err_alloc:
120 if (data->exit)
121 data->exit(&pdev->dev);
122 return ret;
125 static int pwm_backlight_remove(struct platform_device *pdev)
127 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
128 struct backlight_device *bl = platform_get_drvdata(pdev);
129 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
131 backlight_device_unregister(bl);
132 pwm_config(pb->pwm, 0, pb->period);
133 pwm_disable(pb->pwm);
134 pwm_free(pb->pwm);
135 kfree(pb);
136 if (data->exit)
137 data->exit(&pdev->dev);
138 return 0;
141 #ifdef CONFIG_PM
142 static int pwm_backlight_suspend(struct platform_device *pdev,
143 pm_message_t state)
145 struct backlight_device *bl = platform_get_drvdata(pdev);
146 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
148 if (pb->notify)
149 pb->notify(0);
150 pwm_config(pb->pwm, 0, pb->period);
151 pwm_disable(pb->pwm);
152 return 0;
155 static int pwm_backlight_resume(struct platform_device *pdev)
157 struct backlight_device *bl = platform_get_drvdata(pdev);
159 backlight_update_status(bl);
160 return 0;
162 #else
163 #define pwm_backlight_suspend NULL
164 #define pwm_backlight_resume NULL
165 #endif
167 static struct platform_driver pwm_backlight_driver = {
168 .driver = {
169 .name = "pwm-backlight",
170 .owner = THIS_MODULE,
172 .probe = pwm_backlight_probe,
173 .remove = pwm_backlight_remove,
174 .suspend = pwm_backlight_suspend,
175 .resume = pwm_backlight_resume,
178 static int __init pwm_backlight_init(void)
180 return platform_driver_register(&pwm_backlight_driver);
182 module_init(pwm_backlight_init);
184 static void __exit pwm_backlight_exit(void)
186 platform_driver_unregister(&pwm_backlight_driver);
188 module_exit(pwm_backlight_exit);
190 MODULE_DESCRIPTION("PWM based Backlight Driver");
191 MODULE_LICENSE("GPL");
192 MODULE_ALIAS("platform:pwm-backlight");