blacklight: remove redundant spi driver bus initialization
[deliverable/linux.git] / drivers / video / backlight / pwm_bl.c
CommitLineData
42796d37 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
b8cdd877 6 * 2) platform_data being correctly configured
42796d37 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>
5a0e3ad6 22#include <linux/slab.h>
42796d37 23
24struct pwm_bl_data {
25 struct pwm_device *pwm;
cfc3899f 26 struct device *dev;
42796d37 27 unsigned int period;
fef7764f 28 unsigned int lth_brightness;
cfc3899f
BD
29 int (*notify)(struct device *,
30 int brightness);
cc7993f6
DL
31 void (*notify_after)(struct device *,
32 int brightness);
ef0a5e80 33 int (*check_fb)(struct device *, struct fb_info *);
42796d37 34};
35
36static int pwm_backlight_update_status(struct backlight_device *bl)
37{
38 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
39 int brightness = bl->props.brightness;
40 int max = bl->props.max_brightness;
41
42 if (bl->props.power != FB_BLANK_UNBLANK)
43 brightness = 0;
44
45 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
46 brightness = 0;
47
3b73125a 48 if (pb->notify)
cfc3899f 49 brightness = pb->notify(pb->dev, brightness);
3b73125a 50
42796d37 51 if (brightness == 0) {
52 pwm_config(pb->pwm, 0, pb->period);
53 pwm_disable(pb->pwm);
54 } else {
fef7764f
AM
55 brightness = pb->lth_brightness +
56 (brightness * (pb->period - pb->lth_brightness) / max);
57 pwm_config(pb->pwm, brightness, pb->period);
42796d37 58 pwm_enable(pb->pwm);
59 }
cc7993f6
DL
60
61 if (pb->notify_after)
62 pb->notify_after(pb->dev, brightness);
63
42796d37 64 return 0;
65}
66
67static int pwm_backlight_get_brightness(struct backlight_device *bl)
68{
69 return bl->props.brightness;
70}
71
ef0a5e80
RM
72static int pwm_backlight_check_fb(struct backlight_device *bl,
73 struct fb_info *info)
74{
75 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
76
77 return !pb->check_fb || pb->check_fb(pb->dev, info);
78}
79
9905a43b 80static const struct backlight_ops pwm_backlight_ops = {
42796d37 81 .update_status = pwm_backlight_update_status,
82 .get_brightness = pwm_backlight_get_brightness,
ef0a5e80 83 .check_fb = pwm_backlight_check_fb,
42796d37 84};
85
86static int pwm_backlight_probe(struct platform_device *pdev)
87{
a19a6ee6 88 struct backlight_properties props;
42796d37 89 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
90 struct backlight_device *bl;
91 struct pwm_bl_data *pb;
3b73125a 92 int ret;
42796d37 93
14563a4e
BD
94 if (!data) {
95 dev_err(&pdev->dev, "failed to find platform data\n");
42796d37 96 return -EINVAL;
14563a4e 97 }
42796d37 98
3b73125a
PZ
99 if (data->init) {
100 ret = data->init(&pdev->dev);
101 if (ret < 0)
102 return ret;
103 }
104
ce969228 105 pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
3b73125a 106 if (!pb) {
14563a4e 107 dev_err(&pdev->dev, "no memory for state\n");
3b73125a
PZ
108 ret = -ENOMEM;
109 goto err_alloc;
110 }
42796d37 111
112 pb->period = data->pwm_period_ns;
3b73125a 113 pb->notify = data->notify;
cc7993f6 114 pb->notify_after = data->notify_after;
ef0a5e80 115 pb->check_fb = data->check_fb;
fef7764f
AM
116 pb->lth_brightness = data->lth_brightness *
117 (data->pwm_period_ns / data->max_brightness);
cfc3899f 118 pb->dev = &pdev->dev;
42796d37 119
120 pb->pwm = pwm_request(data->pwm_id, "backlight");
43bda1a6 121 if (IS_ERR(pb->pwm)) {
42796d37 122 dev_err(&pdev->dev, "unable to request PWM for backlight\n");
43bda1a6 123 ret = PTR_ERR(pb->pwm);
ce969228 124 goto err_alloc;
14563a4e
BD
125 } else
126 dev_dbg(&pdev->dev, "got pwm for backlight\n");
42796d37 127
a19a6ee6 128 memset(&props, 0, sizeof(struct backlight_properties));
bb7ca747 129 props.type = BACKLIGHT_RAW;
a19a6ee6
MG
130 props.max_brightness = data->max_brightness;
131 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
132 &pwm_backlight_ops, &props);
42796d37 133 if (IS_ERR(bl)) {
134 dev_err(&pdev->dev, "failed to register backlight\n");
3b73125a
PZ
135 ret = PTR_ERR(bl);
136 goto err_bl;
42796d37 137 }
138
42796d37 139 bl->props.brightness = data->dft_brightness;
140 backlight_update_status(bl);
141
142 platform_set_drvdata(pdev, bl);
143 return 0;
3b73125a
PZ
144
145err_bl:
146 pwm_free(pb->pwm);
3b73125a
PZ
147err_alloc:
148 if (data->exit)
149 data->exit(&pdev->dev);
150 return ret;
42796d37 151}
152
153static int pwm_backlight_remove(struct platform_device *pdev)
154{
3b73125a 155 struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
42796d37 156 struct backlight_device *bl = platform_get_drvdata(pdev);
157 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
158
159 backlight_device_unregister(bl);
160 pwm_config(pb->pwm, 0, pb->period);
161 pwm_disable(pb->pwm);
162 pwm_free(pb->pwm);
3b73125a
PZ
163 if (data->exit)
164 data->exit(&pdev->dev);
42796d37 165 return 0;
166}
167
168#ifdef CONFIG_PM
e2c17bc6 169static int pwm_backlight_suspend(struct device *dev)
42796d37 170{
e2c17bc6 171 struct backlight_device *bl = dev_get_drvdata(dev);
42796d37 172 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
173
82e8b542 174 if (pb->notify)
cfc3899f 175 pb->notify(pb->dev, 0);
42796d37 176 pwm_config(pb->pwm, 0, pb->period);
177 pwm_disable(pb->pwm);
cc7993f6
DL
178 if (pb->notify_after)
179 pb->notify_after(pb->dev, 0);
42796d37 180 return 0;
181}
182
e2c17bc6 183static int pwm_backlight_resume(struct device *dev)
42796d37 184{
e2c17bc6 185 struct backlight_device *bl = dev_get_drvdata(dev);
42796d37 186
187 backlight_update_status(bl);
188 return 0;
189}
e2c17bc6
MB
190
191static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend,
192 pwm_backlight_resume);
193
42796d37 194#endif
195
196static struct platform_driver pwm_backlight_driver = {
197 .driver = {
198 .name = "pwm-backlight",
199 .owner = THIS_MODULE,
e2c17bc6
MB
200#ifdef CONFIG_PM
201 .pm = &pwm_backlight_pm_ops,
202#endif
42796d37 203 },
204 .probe = pwm_backlight_probe,
205 .remove = pwm_backlight_remove,
42796d37 206};
207
81178e02 208module_platform_driver(pwm_backlight_driver);
42796d37 209
210MODULE_DESCRIPTION("PWM based Backlight Driver");
211MODULE_LICENSE("GPL");
8cd68198
BD
212MODULE_ALIAS("platform:pwm-backlight");
213
This page took 0.378305 seconds and 5 git commands to generate.