blob: 550443518891d41345234f4aa72929798b1f68a8 [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
Eric Miaob8cdd872009-02-10 13:30:37 +08006 * 2) platform_data being correctly configured
eric miao42796d32008-04-14 09:35:08 +01007 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
eric miao42796d32008-04-14 09:35:08 +010023
24struct pwm_bl_data {
25 struct pwm_device *pwm;
Ben Dookscfc38992009-11-10 17:20:40 +000026 struct device *dev;
eric miao42796d32008-04-14 09:35:08 +010027 unsigned int period;
Ben Dookscfc38992009-11-10 17:20:40 +000028 int (*notify)(struct device *,
29 int brightness);
eric miao42796d32008-04-14 09:35:08 +010030};
31
32static int pwm_backlight_update_status(struct backlight_device *bl)
33{
34 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
35 int brightness = bl->props.brightness;
36 int max = bl->props.max_brightness;
37
38 if (bl->props.power != FB_BLANK_UNBLANK)
39 brightness = 0;
40
41 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
42 brightness = 0;
43
Philipp Zabel3b731252008-05-22 14:18:40 +010044 if (pb->notify)
Ben Dookscfc38992009-11-10 17:20:40 +000045 brightness = pb->notify(pb->dev, brightness);
Philipp Zabel3b731252008-05-22 14:18:40 +010046
eric miao42796d32008-04-14 09:35:08 +010047 if (brightness == 0) {
48 pwm_config(pb->pwm, 0, pb->period);
49 pwm_disable(pb->pwm);
50 } else {
51 pwm_config(pb->pwm, brightness * pb->period / max, pb->period);
52 pwm_enable(pb->pwm);
53 }
54 return 0;
55}
56
57static int pwm_backlight_get_brightness(struct backlight_device *bl)
58{
59 return bl->props.brightness;
60}
61
Emese Revfy9905a432009-12-14 00:58:57 +010062static const struct backlight_ops pwm_backlight_ops = {
eric miao42796d32008-04-14 09:35:08 +010063 .update_status = pwm_backlight_update_status,
64 .get_brightness = pwm_backlight_get_brightness,
65};
66
67static int pwm_backlight_probe(struct platform_device *pdev)
68{
Matthew Garretta19a6ee2010-02-17 16:39:44 -050069 struct backlight_properties props;
eric miao42796d32008-04-14 09:35:08 +010070 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
71 struct backlight_device *bl;
72 struct pwm_bl_data *pb;
Philipp Zabel3b731252008-05-22 14:18:40 +010073 int ret;
eric miao42796d32008-04-14 09:35:08 +010074
Ben Dooks14563a42008-08-05 13:01:22 -070075 if (!data) {
76 dev_err(&pdev->dev, "failed to find platform data\n");
eric miao42796d32008-04-14 09:35:08 +010077 return -EINVAL;
Ben Dooks14563a42008-08-05 13:01:22 -070078 }
eric miao42796d32008-04-14 09:35:08 +010079
Philipp Zabel3b731252008-05-22 14:18:40 +010080 if (data->init) {
81 ret = data->init(&pdev->dev);
82 if (ret < 0)
83 return ret;
84 }
85
eric miao42796d32008-04-14 09:35:08 +010086 pb = kzalloc(sizeof(*pb), GFP_KERNEL);
Philipp Zabel3b731252008-05-22 14:18:40 +010087 if (!pb) {
Ben Dooks14563a42008-08-05 13:01:22 -070088 dev_err(&pdev->dev, "no memory for state\n");
Philipp Zabel3b731252008-05-22 14:18:40 +010089 ret = -ENOMEM;
90 goto err_alloc;
91 }
eric miao42796d32008-04-14 09:35:08 +010092
93 pb->period = data->pwm_period_ns;
Philipp Zabel3b731252008-05-22 14:18:40 +010094 pb->notify = data->notify;
Ben Dookscfc38992009-11-10 17:20:40 +000095 pb->dev = &pdev->dev;
eric miao42796d32008-04-14 09:35:08 +010096
97 pb->pwm = pwm_request(data->pwm_id, "backlight");
Ben Dooks43bda1a2008-07-01 14:18:27 +010098 if (IS_ERR(pb->pwm)) {
eric miao42796d32008-04-14 09:35:08 +010099 dev_err(&pdev->dev, "unable to request PWM for backlight\n");
Ben Dooks43bda1a2008-07-01 14:18:27 +0100100 ret = PTR_ERR(pb->pwm);
Philipp Zabel3b731252008-05-22 14:18:40 +0100101 goto err_pwm;
Ben Dooks14563a42008-08-05 13:01:22 -0700102 } else
103 dev_dbg(&pdev->dev, "got pwm for backlight\n");
eric miao42796d32008-04-14 09:35:08 +0100104
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500105 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);
eric miao42796d32008-04-14 09:35:08 +0100109 if (IS_ERR(bl)) {
110 dev_err(&pdev->dev, "failed to register backlight\n");
Philipp Zabel3b731252008-05-22 14:18:40 +0100111 ret = PTR_ERR(bl);
112 goto err_bl;
eric miao42796d32008-04-14 09:35:08 +0100113 }
114
eric miao42796d32008-04-14 09:35:08 +0100115 bl->props.brightness = data->dft_brightness;
116 backlight_update_status(bl);
117
118 platform_set_drvdata(pdev, bl);
119 return 0;
Philipp Zabel3b731252008-05-22 14:18:40 +0100120
121err_bl:
122 pwm_free(pb->pwm);
123err_pwm:
124 kfree(pb);
125err_alloc:
126 if (data->exit)
127 data->exit(&pdev->dev);
128 return ret;
eric miao42796d32008-04-14 09:35:08 +0100129}
130
131static int pwm_backlight_remove(struct platform_device *pdev)
132{
Philipp Zabel3b731252008-05-22 14:18:40 +0100133 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
eric miao42796d32008-04-14 09:35:08 +0100134 struct backlight_device *bl = platform_get_drvdata(pdev);
135 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
136
137 backlight_device_unregister(bl);
138 pwm_config(pb->pwm, 0, pb->period);
139 pwm_disable(pb->pwm);
140 pwm_free(pb->pwm);
141 kfree(pb);
Philipp Zabel3b731252008-05-22 14:18:40 +0100142 if (data->exit)
143 data->exit(&pdev->dev);
eric miao42796d32008-04-14 09:35:08 +0100144 return 0;
145}
146
147#ifdef CONFIG_PM
148static int pwm_backlight_suspend(struct platform_device *pdev,
149 pm_message_t state)
150{
151 struct backlight_device *bl = platform_get_drvdata(pdev);
152 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
153
Marc Zyngier82e8b542009-06-19 10:50:54 +0200154 if (pb->notify)
Ben Dookscfc38992009-11-10 17:20:40 +0000155 pb->notify(pb->dev, 0);
eric miao42796d32008-04-14 09:35:08 +0100156 pwm_config(pb->pwm, 0, pb->period);
157 pwm_disable(pb->pwm);
158 return 0;
159}
160
161static int pwm_backlight_resume(struct platform_device *pdev)
162{
163 struct backlight_device *bl = platform_get_drvdata(pdev);
164
165 backlight_update_status(bl);
166 return 0;
167}
168#else
169#define pwm_backlight_suspend NULL
170#define pwm_backlight_resume NULL
171#endif
172
173static struct platform_driver pwm_backlight_driver = {
174 .driver = {
175 .name = "pwm-backlight",
176 .owner = THIS_MODULE,
177 },
178 .probe = pwm_backlight_probe,
179 .remove = pwm_backlight_remove,
180 .suspend = pwm_backlight_suspend,
181 .resume = pwm_backlight_resume,
182};
183
184static int __init pwm_backlight_init(void)
185{
186 return platform_driver_register(&pwm_backlight_driver);
187}
188module_init(pwm_backlight_init);
189
190static void __exit pwm_backlight_exit(void)
191{
192 platform_driver_unregister(&pwm_backlight_driver);
193}
194module_exit(pwm_backlight_exit);
195
196MODULE_DESCRIPTION("PWM based Backlight Driver");
197MODULE_LICENSE("GPL");
Ben Dooks8cd68192008-08-05 13:01:24 -0700198MODULE_ALIAS("platform:pwm-backlight");
199