blob: 9637f5e08cb6242f17f2d27bc1d41c8c37bb4ccf [file] [log] [blame]
eric miao42796d32008-04-14 09:35:08 +01001/*
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
6 * 2) platform_data casts to the PWM id (0/1/2/3 on PXA)
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
23struct pwm_bl_data {
24 struct pwm_device *pwm;
25 unsigned int period;
26};
27
28static int pwm_backlight_update_status(struct backlight_device *bl)
29{
30 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
31 int brightness = bl->props.brightness;
32 int max = bl->props.max_brightness;
33
34 if (bl->props.power != FB_BLANK_UNBLANK)
35 brightness = 0;
36
37 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
38 brightness = 0;
39
40 if (brightness == 0) {
41 pwm_config(pb->pwm, 0, pb->period);
42 pwm_disable(pb->pwm);
43 } else {
44 pwm_config(pb->pwm, brightness * pb->period / max, pb->period);
45 pwm_enable(pb->pwm);
46 }
47 return 0;
48}
49
50static int pwm_backlight_get_brightness(struct backlight_device *bl)
51{
52 return bl->props.brightness;
53}
54
55static struct backlight_ops pwm_backlight_ops = {
56 .update_status = pwm_backlight_update_status,
57 .get_brightness = pwm_backlight_get_brightness,
58};
59
60static int pwm_backlight_probe(struct platform_device *pdev)
61{
62 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
63 struct backlight_device *bl;
64 struct pwm_bl_data *pb;
65
66 if (!data)
67 return -EINVAL;
68
69 pb = kzalloc(sizeof(*pb), GFP_KERNEL);
70 if (!pb)
71 return -ENOMEM;
72
73 pb->period = data->pwm_period_ns;
74
75 pb->pwm = pwm_request(data->pwm_id, "backlight");
76 if (pb->pwm == NULL) {
77 dev_err(&pdev->dev, "unable to request PWM for backlight\n");
78 kfree(pb);
79 return -EBUSY;
80 }
81
82 bl = backlight_device_register(pdev->name, &pdev->dev,
83 pb, &pwm_backlight_ops);
84 if (IS_ERR(bl)) {
85 dev_err(&pdev->dev, "failed to register backlight\n");
86 pwm_free(pb->pwm);
87 kfree(pb);
88 return PTR_ERR(bl);
89 }
90
91 bl->props.max_brightness = data->max_brightness;
92 bl->props.brightness = data->dft_brightness;
93 backlight_update_status(bl);
94
95 platform_set_drvdata(pdev, bl);
96 return 0;
97}
98
99static int pwm_backlight_remove(struct platform_device *pdev)
100{
101 struct backlight_device *bl = platform_get_drvdata(pdev);
102 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
103
104 backlight_device_unregister(bl);
105 pwm_config(pb->pwm, 0, pb->period);
106 pwm_disable(pb->pwm);
107 pwm_free(pb->pwm);
108 kfree(pb);
109 return 0;
110}
111
112#ifdef CONFIG_PM
113static int pwm_backlight_suspend(struct platform_device *pdev,
114 pm_message_t state)
115{
116 struct backlight_device *bl = platform_get_drvdata(pdev);
117 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
118
119 pwm_config(pb->pwm, 0, pb->period);
120 pwm_disable(pb->pwm);
121 return 0;
122}
123
124static int pwm_backlight_resume(struct platform_device *pdev)
125{
126 struct backlight_device *bl = platform_get_drvdata(pdev);
127
128 backlight_update_status(bl);
129 return 0;
130}
131#else
132#define pwm_backlight_suspend NULL
133#define pwm_backlight_resume NULL
134#endif
135
136static struct platform_driver pwm_backlight_driver = {
137 .driver = {
138 .name = "pwm-backlight",
139 .owner = THIS_MODULE,
140 },
141 .probe = pwm_backlight_probe,
142 .remove = pwm_backlight_remove,
143 .suspend = pwm_backlight_suspend,
144 .resume = pwm_backlight_resume,
145};
146
147static int __init pwm_backlight_init(void)
148{
149 return platform_driver_register(&pwm_backlight_driver);
150}
151module_init(pwm_backlight_init);
152
153static void __exit pwm_backlight_exit(void)
154{
155 platform_driver_unregister(&pwm_backlight_driver);
156}
157module_exit(pwm_backlight_exit);
158
159MODULE_DESCRIPTION("PWM based Backlight Driver");
160MODULE_LICENSE("GPL");