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>
22 #include <linux/slab.h>
25 struct pwm_device
*pwm
;
28 int (*notify
)(struct device
*,
32 static int pwm_backlight_update_status(struct backlight_device
*bl
)
34 struct pwm_bl_data
*pb
= dev_get_drvdata(&bl
->dev
);
35 int brightness
= bl
->props
.brightness
;
36 int max
= bl
->props
.max_brightness
;
38 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
41 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
45 brightness
= pb
->notify(pb
->dev
, brightness
);
47 if (brightness
== 0) {
48 pwm_config(pb
->pwm
, 0, pb
->period
);
51 pwm_config(pb
->pwm
, brightness
* pb
->period
/ max
, pb
->period
);
57 static int pwm_backlight_get_brightness(struct backlight_device
*bl
)
59 return bl
->props
.brightness
;
62 static const struct backlight_ops pwm_backlight_ops
= {
63 .update_status
= pwm_backlight_update_status
,
64 .get_brightness
= pwm_backlight_get_brightness
,
67 static int pwm_backlight_probe(struct platform_device
*pdev
)
69 struct backlight_properties props
;
70 struct platform_pwm_backlight_data
*data
= pdev
->dev
.platform_data
;
71 struct backlight_device
*bl
;
72 struct pwm_bl_data
*pb
;
76 dev_err(&pdev
->dev
, "failed to find platform data\n");
81 ret
= data
->init(&pdev
->dev
);
86 pb
= kzalloc(sizeof(*pb
), GFP_KERNEL
);
88 dev_err(&pdev
->dev
, "no memory for state\n");
93 pb
->period
= data
->pwm_period_ns
;
94 pb
->notify
= data
->notify
;
97 pb
->pwm
= pwm_request(data
->pwm_id
, "backlight");
98 if (IS_ERR(pb
->pwm
)) {
99 dev_err(&pdev
->dev
, "unable to request PWM for backlight\n");
100 ret
= PTR_ERR(pb
->pwm
);
103 dev_dbg(&pdev
->dev
, "got pwm for backlight\n");
105 memset(&props
, 0, sizeof(struct backlight_properties
));
106 props
.max_brightness
= data
->max_brightness
;
107 bl
= backlight_device_register(dev_name(&pdev
->dev
), &pdev
->dev
, pb
,
108 &pwm_backlight_ops
, &props
);
110 dev_err(&pdev
->dev
, "failed to register backlight\n");
115 bl
->props
.brightness
= data
->dft_brightness
;
116 backlight_update_status(bl
);
118 platform_set_drvdata(pdev
, bl
);
127 data
->exit(&pdev
->dev
);
131 static int pwm_backlight_remove(struct platform_device
*pdev
)
133 struct platform_pwm_backlight_data
*data
= pdev
->dev
.platform_data
;
134 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
135 struct pwm_bl_data
*pb
= dev_get_drvdata(&bl
->dev
);
137 backlight_device_unregister(bl
);
138 pwm_config(pb
->pwm
, 0, pb
->period
);
139 pwm_disable(pb
->pwm
);
143 data
->exit(&pdev
->dev
);
148 static int pwm_backlight_suspend(struct platform_device
*pdev
,
151 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
152 struct pwm_bl_data
*pb
= dev_get_drvdata(&bl
->dev
);
155 pb
->notify(pb
->dev
, 0);
156 pwm_config(pb
->pwm
, 0, pb
->period
);
157 pwm_disable(pb
->pwm
);
161 static int pwm_backlight_resume(struct platform_device
*pdev
)
163 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
165 backlight_update_status(bl
);
169 #define pwm_backlight_suspend NULL
170 #define pwm_backlight_resume NULL
173 static struct platform_driver pwm_backlight_driver
= {
175 .name
= "pwm-backlight",
176 .owner
= THIS_MODULE
,
178 .probe
= pwm_backlight_probe
,
179 .remove
= pwm_backlight_remove
,
180 .suspend
= pwm_backlight_suspend
,
181 .resume
= pwm_backlight_resume
,
184 static int __init
pwm_backlight_init(void)
186 return platform_driver_register(&pwm_backlight_driver
);
188 module_init(pwm_backlight_init
);
190 static void __exit
pwm_backlight_exit(void)
192 platform_driver_unregister(&pwm_backlight_driver
);
194 module_exit(pwm_backlight_exit
);
196 MODULE_DESCRIPTION("PWM based Backlight Driver");
197 MODULE_LICENSE("GPL");
198 MODULE_ALIAS("platform:pwm-backlight");