xref: /linux/drivers/video/backlight/pwm_bl.c (revision bb7ca747f8d6243b3943c5b133048652020f4a50)
1 /*
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 being correctly configured
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 #include <linux/slab.h>
23 
24 struct pwm_bl_data {
25 	struct pwm_device	*pwm;
26 	struct device		*dev;
27 	unsigned int		period;
28 	unsigned int		lth_brightness;
29 	int			(*notify)(struct device *,
30 					  int brightness);
31 };
32 
33 static int pwm_backlight_update_status(struct backlight_device *bl)
34 {
35 	struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
36 	int brightness = bl->props.brightness;
37 	int max = bl->props.max_brightness;
38 
39 	if (bl->props.power != FB_BLANK_UNBLANK)
40 		brightness = 0;
41 
42 	if (bl->props.fb_blank != FB_BLANK_UNBLANK)
43 		brightness = 0;
44 
45 	if (pb->notify)
46 		brightness = pb->notify(pb->dev, brightness);
47 
48 	if (brightness == 0) {
49 		pwm_config(pb->pwm, 0, pb->period);
50 		pwm_disable(pb->pwm);
51 	} else {
52 		brightness = pb->lth_brightness +
53 			(brightness * (pb->period - pb->lth_brightness) / max);
54 		pwm_config(pb->pwm, brightness, pb->period);
55 		pwm_enable(pb->pwm);
56 	}
57 	return 0;
58 }
59 
60 static int pwm_backlight_get_brightness(struct backlight_device *bl)
61 {
62 	return bl->props.brightness;
63 }
64 
65 static const struct backlight_ops pwm_backlight_ops = {
66 	.update_status	= pwm_backlight_update_status,
67 	.get_brightness	= pwm_backlight_get_brightness,
68 };
69 
70 static int pwm_backlight_probe(struct platform_device *pdev)
71 {
72 	struct backlight_properties props;
73 	struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
74 	struct backlight_device *bl;
75 	struct pwm_bl_data *pb;
76 	int ret;
77 
78 	if (!data) {
79 		dev_err(&pdev->dev, "failed to find platform data\n");
80 		return -EINVAL;
81 	}
82 
83 	if (data->init) {
84 		ret = data->init(&pdev->dev);
85 		if (ret < 0)
86 			return ret;
87 	}
88 
89 	pb = kzalloc(sizeof(*pb), GFP_KERNEL);
90 	if (!pb) {
91 		dev_err(&pdev->dev, "no memory for state\n");
92 		ret = -ENOMEM;
93 		goto err_alloc;
94 	}
95 
96 	pb->period = data->pwm_period_ns;
97 	pb->notify = data->notify;
98 	pb->lth_brightness = data->lth_brightness *
99 		(data->pwm_period_ns / data->max_brightness);
100 	pb->dev = &pdev->dev;
101 
102 	pb->pwm = pwm_request(data->pwm_id, "backlight");
103 	if (IS_ERR(pb->pwm)) {
104 		dev_err(&pdev->dev, "unable to request PWM for backlight\n");
105 		ret = PTR_ERR(pb->pwm);
106 		goto err_pwm;
107 	} else
108 		dev_dbg(&pdev->dev, "got pwm for backlight\n");
109 
110 	memset(&props, 0, sizeof(struct backlight_properties));
111 	props.type = BACKLIGHT_RAW;
112 	props.max_brightness = data->max_brightness;
113 	bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
114 				       &pwm_backlight_ops, &props);
115 	if (IS_ERR(bl)) {
116 		dev_err(&pdev->dev, "failed to register backlight\n");
117 		ret = PTR_ERR(bl);
118 		goto err_bl;
119 	}
120 
121 	bl->props.brightness = data->dft_brightness;
122 	backlight_update_status(bl);
123 
124 	platform_set_drvdata(pdev, bl);
125 	return 0;
126 
127 err_bl:
128 	pwm_free(pb->pwm);
129 err_pwm:
130 	kfree(pb);
131 err_alloc:
132 	if (data->exit)
133 		data->exit(&pdev->dev);
134 	return ret;
135 }
136 
137 static int pwm_backlight_remove(struct platform_device *pdev)
138 {
139 	struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
140 	struct backlight_device *bl = platform_get_drvdata(pdev);
141 	struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
142 
143 	backlight_device_unregister(bl);
144 	pwm_config(pb->pwm, 0, pb->period);
145 	pwm_disable(pb->pwm);
146 	pwm_free(pb->pwm);
147 	kfree(pb);
148 	if (data->exit)
149 		data->exit(&pdev->dev);
150 	return 0;
151 }
152 
153 #ifdef CONFIG_PM
154 static int pwm_backlight_suspend(struct platform_device *pdev,
155 				 pm_message_t state)
156 {
157 	struct backlight_device *bl = platform_get_drvdata(pdev);
158 	struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
159 
160 	if (pb->notify)
161 		pb->notify(pb->dev, 0);
162 	pwm_config(pb->pwm, 0, pb->period);
163 	pwm_disable(pb->pwm);
164 	return 0;
165 }
166 
167 static int pwm_backlight_resume(struct platform_device *pdev)
168 {
169 	struct backlight_device *bl = platform_get_drvdata(pdev);
170 
171 	backlight_update_status(bl);
172 	return 0;
173 }
174 #else
175 #define pwm_backlight_suspend	NULL
176 #define pwm_backlight_resume	NULL
177 #endif
178 
179 static struct platform_driver pwm_backlight_driver = {
180 	.driver		= {
181 		.name	= "pwm-backlight",
182 		.owner	= THIS_MODULE,
183 	},
184 	.probe		= pwm_backlight_probe,
185 	.remove		= pwm_backlight_remove,
186 	.suspend	= pwm_backlight_suspend,
187 	.resume		= pwm_backlight_resume,
188 };
189 
190 static int __init pwm_backlight_init(void)
191 {
192 	return platform_driver_register(&pwm_backlight_driver);
193 }
194 module_init(pwm_backlight_init);
195 
196 static void __exit pwm_backlight_exit(void)
197 {
198 	platform_driver_unregister(&pwm_backlight_driver);
199 }
200 module_exit(pwm_backlight_exit);
201 
202 MODULE_DESCRIPTION("PWM based Backlight Driver");
203 MODULE_LICENSE("GPL");
204 MODULE_ALIAS("platform:pwm-backlight");
205 
206