1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*++
linux/drivers/video/backlight/wmt_bl.c
Copyright (c) 2013 WonderMedia Technologies, Inc.
This program is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software Foundation,
either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
WonderMedia Technologies, Inc.
10F, 529, Chung-Cheng Road, Hsin-Tien, Taipei 231, R.O.C.
--*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/pwm_backlight.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/leds.h>
#include <mach/wmt_env.h>
#include "wmt_bl.h"
#define LTH_BRIGHTNESS 0
#define DFT_BRIGHTNESS 180
#define DFT_PWM0_FREQ 4 /* kHz */
static struct {
int gpio;
int active;
} power = { -1, 0, };
static int backlight_delay = 0;
static struct led_trigger *genesis_led;
static BLOCKING_NOTIFIER_HEAD(bl_chain_head);
int register_bl_notifier(struct notifier_block *nb)
{
return blocking_notifier_chain_register(&bl_chain_head, nb);
}
EXPORT_SYMBOL_GPL(register_bl_notifier);
int unregister_bl_notifier(struct notifier_block *nb)
{
return blocking_notifier_chain_unregister(&bl_chain_head, nb);
}
EXPORT_SYMBOL_GPL(unregister_bl_notifier);
static inline int bl_notifier_call_chain(unsigned long val)
{
int ret = blocking_notifier_call_chain(&bl_chain_head, val, NULL);
return notifier_to_errno(ret);
}
static void backlight_on(bool on)
{
static int old_state = -1;
if (!gpio_is_valid(power.gpio))
return;
if (old_state != on) {
if (old_state == false) {
/* delay 100 ms between 'lcd power on' and 'backlight
* power on' at resume sequence. */
msleep(100);
/* wmt.backlight.delay */
msleep(backlight_delay);
}
gpio_direction_output(power.gpio, on ? power.active : !power.active);
led_trigger_event(genesis_led, on ? LED_FULL : LED_OFF);
old_state = on;
bl_notifier_call_chain(on ? BL_OPEN : BL_CLOSE);
}
}
static int wmt_bl_notify(struct device *dev, int brightness)
{
return brightness;
}
static void wmt_bl_notify_after(struct device *dev, int brightness)
{
backlight_on(brightness != 0);
}
#define KHZ2PICOS(hz) (1000000000/(hz))
static struct platform_pwm_backlight_data wm8880_pwmbl_data = {
.pwm_id = 0,
.invert = 1,
.lth_brightness = LTH_BRIGHTNESS, /* low threshold */
.hth_brightness = 256, /* high threshold */
.max_brightness = 256,
.dft_brightness = DFT_BRIGHTNESS,
.pwm_period_ns = KHZ2PICOS(DFT_PWM0_FREQ*1000), /* revisit when clocks are implemented */
.notify = wmt_bl_notify,
.notify_after = wmt_bl_notify_after,
};
struct platform_device wm8880_device_pwmbl = {
.name = "pwm-backlight",
.id = 0,
.dev = {
.platform_data = &wm8880_pwmbl_data,
},
};
static int parse_backlight_param(void)
{
static char env[] = "wmt.backlight.param";
struct platform_pwm_backlight_data *pdata = &wm8880_pwmbl_data;
uint8_t buf[64];
size_t l = sizeof(buf);
int pwm_freq = DFT_PWM0_FREQ;
int rc;
if (wmt_getsyspara(env, buf, &l)) {
pr_err("please set %s\n", env);
return -EINVAL;
}
/* wmt.backlight.param
* <pwmid>:<invert>:<gpio>:<active>:
* <lth_brightness>:<hth_brightness>:<pwm0freq>:<dft_brightness>
*/
rc = sscanf(buf, "%d:%d:%d:%d:%d:%d:%d:%d",
&pdata->pwm_id,
&pdata->invert,
&power.gpio, &power.active,
&pdata->lth_brightness,
&pdata->hth_brightness,
&pwm_freq,
&pdata->dft_brightness);
if ((rc < 4)) {
pr_err("bad env %s %s\n", env, buf);
return -EINVAL;
}
if ((pdata->hth_brightness > pdata->max_brightness) ||
(pdata->lth_brightness > pdata->hth_brightness))
pdata->hth_brightness = pdata->max_brightness;
pdata->pwm_period_ns = KHZ2PICOS(pwm_freq * 1000);
pr_info("backlight param: pwm%d(%dkHz), invert %d, brightness %d(%d~%d) ",
pdata->pwm_id, pwm_freq, pdata->invert, pdata->dft_brightness,
pdata->lth_brightness, pdata->hth_brightness);
if (gpio_is_valid(power.gpio)) {
if (gpio_request(power.gpio, "pwm_bl switch")) {
pr_warning("gpio request %d failed\n", power.gpio);
power.gpio = -1;
}
pr_info("gpio%d (active %d)\n", power.gpio, power.active);
} else
pr_info("no gpio control\n");
/* parse wmt.backlight.delay */
l = sizeof(buf);
if (wmt_getsyspara("wmt.backlight.delay", buf, &l) == 0) {
sscanf(buf, "%d", &backlight_delay);
}
pr_info("backlight delay: %dms\n", backlight_delay);
return 0;
}
static int __init wmt_bl_init(void)
{
int rc;
rc = parse_backlight_param();
if (rc)
return rc;
led_trigger_register_simple("genesis", &genesis_led);
return platform_device_register(&wm8880_device_pwmbl);
}
static void __exit wmt_bl_exit(void)
{
led_trigger_unregister_simple(genesis_led);
platform_device_unregister(&wm8880_device_pwmbl);
}
module_init(wmt_bl_init);
module_exit(wmt_bl_exit);
|