summaryrefslogtreecommitdiff
path: root/drivers/leds/wmt-lcd-backlight.c
blob: d51b7c5b7898ac4f5e1dabe9e602d9cac3dfb7c2 (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**************************************************************
Copyright (c) 2008 WonderMedia Technologies, Inc.

Module Name:
    $Workfile: wmt-lcd-backlight.c $
Abstract:
    This program is the WMT LCD backlight driver for Android 1.6 system.
    Andriod1.6 API adjusts the LCD backlight by writing follwing file:
        /sys/class/leds/lcd-backlight/brightness
    Use WMT PWM to control the LCD backlight
Revision History:
    Jan.08.2010 First Created by HowayHuo
	
**************************************************************/
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/leds.h>
#include <linux/io.h>
#include <linux/slab.h>
#include "../char/wmt-pwm.h"

#define BACKLIGHT_ON 0
#define BACKLIGHT_CLOSE	1

struct wmt_pwm_reg_t {
	unsigned int id;
	unsigned int scalar;
	unsigned int period;
	unsigned int duty;
	unsigned int config;
	unsigned int status;
};

static struct wmt_pwm_reg_t g_pwm_setting;

#define ENV_LCD_BACKLIGHT "wmt.display.pwm"

extern int wmt_getsyspara(char *varname, unsigned char *varval, int *varlen);

struct back_light_limit {
	int max;
	int min;
	int diff;
	int step;
	int allheigh;
};

struct back_light_limit g_backlight_limit;

static unsigned int g_oldbrightness;
static unsigned int g_isresume = 0;

extern void vpp_set_lvds_blank(int blank);


void backlight_get_env(void)
{
	unsigned char buf[100];
	int varlen = 100;
	int max = -1, min = -1;
	int val = -1;

	memset((char *)&g_backlight_limit, 0, sizeof(struct back_light_limit));
	if( wmt_getsyspara(ENV_LCD_BACKLIGHT,buf,&varlen) == 0) {
		//printk("\r\n backlight_get_env : %s = %s \n",ENV_LCD_BACKLIGHT,buf);
		sscanf(buf,"%x",&val);
		g_pwm_setting.id = val&0x0F;
		if (g_pwm_setting.id > 1) {
			printk("PWM%d is invaild , using default PWM0\n", g_pwm_setting.id);
			g_pwm_setting.id = 0;
		}		
		min = (val >> 8)&0xff;
		max = (val >> 16)&0xff;
		if ((min >= max) || (max > 100) || (min < 0)) {
			printk("backlight_get_env error : max = %d , min = %d ", max, min);
			goto out;
		}
		g_backlight_limit.max = max;
		g_backlight_limit.min = min;
	} else {
		printk("## Warning: %s not defined\n",ENV_LCD_BACKLIGHT);
		goto out;
	}
	return;
out:
	g_pwm_setting.id = 0;
	g_backlight_limit.max = 90;
	g_backlight_limit.min = 10;
}


/*
 * For simplicity, we use "brightness" as if it were a linear function
 * of PWM duty cycle.  However, a logarithmic function of duty cycle is
 * probably a better match for perceived brightness: two is half as bright
 * as four, four is half as bright as eight, etc
 */
static void lcd_brightness(struct led_classdev *cdev, enum led_brightness b)
{
    unsigned int duty;
    int val = 0;

	/*printk(KERN_ALERT "[%s]pwm_no = %d pwm_period = %d b= %d\n", __func__, pwm_no,pwm_period, b);*/

	if (g_isresume == 1) {
		if (b <= g_oldbrightness)
			return;
		g_isresume = 0;
	}
	g_oldbrightness = b;
	if (!b) {
		g_pwm_setting.status = BACKLIGHT_CLOSE;		
		pwm_set_gpio(g_pwm_setting.id, 0);
		/*vpp_set_lvds_blank(1);*/
		return;
	}

	if ((b != 255) || (!g_backlight_limit.allheigh)) {
		val = (b*g_backlight_limit.diff*1000)/255;
		duty = (val * g_backlight_limit.step) /1000;
		duty = (duty + g_backlight_limit.min)/1000;
    	if(duty) {
			if(--duty > 0xFFF)
	    		duty = 0xFFF;
    	}
	} else
		duty = g_backlight_limit.max/1000;

	//printk("max = %d , min = %d , b= %d , duty = %d\n", g_backlight_limit.max, g_backlight_limit.min, b,duty);
	duty = duty-1;
 
    pwm_set_duty(g_pwm_setting.id, duty);

	//fix : framework do not set brightness to 0 when suspend
	//if (g_pwm_setting.status == BACKLIGHT_CLOSE) {
	if (!get_lcd_power()) {
		/*vpp_set_lvds_blank(0);*/
		pwm_set_gpio(g_pwm_setting.id, 1);
		g_pwm_setting.status = BACKLIGHT_ON;
	}
}

/*
 * NOTE:  we reuse the platform_data structure of GPIO leds,
 * but repurpose its "gpio" number as a PWM channel number.
 */
static int lcd_backlight_probe(struct platform_device *pdev)
{
	const struct gpio_led_platform_data	*pdata;
	struct led_classdev	*leds;
	int	i;
	int	status;
	unsigned int brightness;
	unsigned int pwm_period;

	pdata = pdev->dev.platform_data;
	if (!pdata || pdata->num_leds < 1)
		return -ENODEV;

	leds = kcalloc(pdata->num_leds, sizeof(*leds), GFP_KERNEL);
	if (!leds)
		return -ENOMEM;

	auto_pll_divisor(DEV_PWM,CLK_ENABLE,0,0);

	pwm_period  = pwm_get_period(g_pwm_setting.id) + 1;
	pwm_period = pwm_period*1000;
	backlight_get_env();
	g_backlight_limit.allheigh = 0;
	if (g_backlight_limit.max == 100)
		g_backlight_limit.allheigh = 1;
	g_backlight_limit.diff = (g_backlight_limit.max - g_backlight_limit.min);
	g_backlight_limit.max = (pwm_period*g_backlight_limit.max)/100;
	g_backlight_limit.min = (pwm_period*g_backlight_limit.min)/100;
	g_backlight_limit.step = (g_backlight_limit.max - g_backlight_limit.min) / g_backlight_limit.diff;

	/*calculate the default brightness*/
	brightness = (pwm_get_duty(g_pwm_setting.id) * 255) / pwm_period;

	for (i = 0; i < pdata->num_leds; i++) {
		struct led_classdev		*led = leds + i;
		const struct gpio_led	*dat = pdata->leds + i;

		led->name = dat->name;
		led->brightness = brightness;
		led->brightness_set = lcd_brightness;
		led->default_trigger = dat->default_trigger;

		/* Hand it over to the LED framework */
		status = led_classdev_register(&pdev->dev, led);
		if (status < 0) {
			goto err;
		}
	}

	platform_set_drvdata(pdev, leds);
	g_pwm_setting.status = BACKLIGHT_ON;
	return 0;

err:
	if (i > 0) {
		for (i = i - 1; i >= 0; i--) {
			led_classdev_unregister(leds + i);
		}
	}
	kfree(leds);

	return status;
}

static int __exit lcd_backlight_remove(struct platform_device *pdev)
{
	const struct gpio_led_platform_data	*pdata;
	struct led_classdev				*leds;
	unsigned				i;

	pdata = pdev->dev.platform_data;
	leds = platform_get_drvdata(pdev);

	for (i = 0; i < pdata->num_leds; i++) {
		struct led_classdev		*led = leds + i;

		led_classdev_unregister(led);
	}

	kfree(leds);
	platform_set_drvdata(pdev, NULL);
	return 0;
}

static int lcd_backlight_suspend
(
	struct platform_device *pdev,     /*!<; // a pointer point to struct device */
	pm_message_t state		/*!<; // suspend state */
)
{
	unsigned int addr;

	addr = PWM_CTRL_REG_ADDR + (0x10 * g_pwm_setting.id);
	g_pwm_setting.config = (REG32_VAL(addr) & 0xFFF);
	pwm_set_control(g_pwm_setting.id, 0);
	addr = PWM_PERIOD_REG_ADDR + (0x10 * g_pwm_setting.id);
	g_pwm_setting.period = (REG32_VAL(addr) & 0xFFF);

	addr = PWM_SCALAR_REG_ADDR + (0x10 * g_pwm_setting.id);
	g_pwm_setting.scalar = (REG32_VAL(addr) & 0xFFF);
	//g_pwm_setting.duty = 0; // for android , AP will set duty to 0

	return 0;
}

static int lcd_backlight_resume
(
	struct platform_device *pdev 	/*!<; // a pointer point to struct device */
)
{
	pwm_set_scalar(g_pwm_setting.id, g_pwm_setting.scalar);
	pwm_set_period(g_pwm_setting.id, g_pwm_setting.period);

	pwm_set_duty(g_pwm_setting.id, 5);
	pwm_set_control(g_pwm_setting.id, g_pwm_setting.config);

	g_isresume = 1;

	return 0;
}


static struct gpio_led lcd_pwm[] = {
	{
		.name			= "lcd-backlight",
	}, 
};


static struct gpio_led_platform_data lcd_backlight_data = {
	.leds		= lcd_pwm,
	.num_leds	= ARRAY_SIZE(lcd_pwm),
};

static struct platform_device lcd_backlight_device = {
	.name           = "lcd-backlight",
	.id             = 0,
	.dev            = 	{	.platform_data = &lcd_backlight_data,			
	},
};

static struct platform_driver lcd_backlight_driver = {
	.driver = {
		.name =		"lcd-backlight",
		.owner =	THIS_MODULE,
	},
	/* REVISIT add suspend() and resume() methods */
	.probe	=	lcd_backlight_probe,
	.remove =	__exit_p(lcd_backlight_remove),
	.suspend        = lcd_backlight_suspend,
	.resume         = lcd_backlight_resume
};

static int __init lcd_backlight_init(void)
{
	int ret;

	ret = platform_device_register(&lcd_backlight_device);
	if(ret) {
		printk("[lcd_backlight_init]Error: Can not register LCD backlight device\n");
		return ret;
	}
	//ret = platform_driver_probe(&lcd_backlight_driver, lcd_backlight_probe);
	ret = platform_driver_register(&lcd_backlight_driver);
	if(ret) {
		printk("[lcd_backlight_init]Error: Can not register LCD backlight driver\n");
		platform_device_unregister(&lcd_backlight_device);
		return ret;
	}
	return 0; 
}
module_init(lcd_backlight_init);

static void __exit lcd_backlight_exit(void)
{
	platform_driver_unregister(&lcd_backlight_driver);
	platform_device_unregister(&lcd_backlight_device);
}
module_exit(lcd_backlight_exit);

MODULE_DESCRIPTION("Driver for LCD with PWM-controlled brightness");
MODULE_LICENSE("GPL");