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>
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
;
27 int (*notify
)(struct device
*,
31 static int pwm_backlight_update_status(struct backlight_device
*bl
)
33 struct pwm_bl_data
*pb
= dev_get_drvdata(&bl
->dev
);
34 int brightness
= bl
->props
.brightness
;
35 int max
= bl
->props
.max_brightness
;
37 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
40 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
44 brightness
= pb
->notify(pb
->dev
, brightness
);
46 if (brightness
== 0) {
47 pwm_config(pb
->pwm
, 0, pb
->period
);
50 pwm_config(pb
->pwm
, brightness
* pb
->period
/ max
, pb
->period
);
56 static int pwm_backlight_get_brightness(struct backlight_device
*bl
)
58 return bl
->props
.brightness
;
61 static const struct backlight_ops pwm_backlight_ops
= {
62 .update_status
= pwm_backlight_update_status
,
63 .get_brightness
= pwm_backlight_get_brightness
,
66 static int pwm_backlight_probe(struct platform_device
*pdev
)
68 struct platform_pwm_backlight_data
*data
= pdev
->dev
.platform_data
;
69 struct backlight_device
*bl
;
70 struct pwm_bl_data
*pb
;
74 dev_err(&pdev
->dev
, "failed to find platform data\n");
79 ret
= data
->init(&pdev
->dev
);
84 pb
= kzalloc(sizeof(*pb
), GFP_KERNEL
);
86 dev_err(&pdev
->dev
, "no memory for state\n");
91 pb
->period
= data
->pwm_period_ns
;
92 pb
->notify
= data
->notify
;
95 pb
->pwm
= pwm_request(data
->pwm_id
, "backlight");
96 if (IS_ERR(pb
->pwm
)) {
97 dev_err(&pdev
->dev
, "unable to request PWM for backlight\n");
98 ret
= PTR_ERR(pb
->pwm
);
101 dev_dbg(&pdev
->dev
, "got pwm for backlight\n");
103 bl
= backlight_device_register(dev_name(&pdev
->dev
), &pdev
->dev
,
104 pb
, &pwm_backlight_ops
);
106 dev_err(&pdev
->dev
, "failed to register backlight\n");
111 bl
->props
.max_brightness
= data
->max_brightness
;
112 bl
->props
.brightness
= data
->dft_brightness
;
113 backlight_update_status(bl
);
115 platform_set_drvdata(pdev
, bl
);
124 data
->exit(&pdev
->dev
);
128 static int pwm_backlight_remove(struct platform_device
*pdev
)
130 struct platform_pwm_backlight_data
*data
= pdev
->dev
.platform_data
;
131 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
132 struct pwm_bl_data
*pb
= dev_get_drvdata(&bl
->dev
);
134 backlight_device_unregister(bl
);
135 pwm_config(pb
->pwm
, 0, pb
->period
);
136 pwm_disable(pb
->pwm
);
140 data
->exit(&pdev
->dev
);
145 static int pwm_backlight_suspend(struct platform_device
*pdev
,
148 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
149 struct pwm_bl_data
*pb
= dev_get_drvdata(&bl
->dev
);
152 pb
->notify(pb
->dev
, 0);
153 pwm_config(pb
->pwm
, 0, pb
->period
);
154 pwm_disable(pb
->pwm
);
158 static int pwm_backlight_resume(struct platform_device
*pdev
)
160 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
162 backlight_update_status(bl
);
166 #define pwm_backlight_suspend NULL
167 #define pwm_backlight_resume NULL
170 static struct platform_driver pwm_backlight_driver
= {
172 .name
= "pwm-backlight",
173 .owner
= THIS_MODULE
,
175 .probe
= pwm_backlight_probe
,
176 .remove
= pwm_backlight_remove
,
177 .suspend
= pwm_backlight_suspend
,
178 .resume
= pwm_backlight_resume
,
181 static int __init
pwm_backlight_init(void)
183 return platform_driver_register(&pwm_backlight_driver
);
185 module_init(pwm_backlight_init
);
187 static void __exit
pwm_backlight_exit(void)
189 platform_driver_unregister(&pwm_backlight_driver
);
191 module_exit(pwm_backlight_exit
);
193 MODULE_DESCRIPTION("PWM based Backlight Driver");
194 MODULE_LICENSE("GPL");
195 MODULE_ALIAS("platform:pwm-backlight");