summaryrefslogtreecommitdiff
path: root/common/wmt_display/pwm.c
blob: d19cd50358e0e50c5d063ef944625c5c23f3f9c7 (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
/*++
 * linux/drivers/video/wmt/lcd.c
 * WonderMedia video post processor (VPP) driver
 *
 * Copyright c 2010  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.
 * 4F, 533, Chung-Cheng Road, Hsin-Tien, Taipei 231, R.O.C
--*/

/*
 * ChangeLog
 *
 * 2010-08-05  Sam Shen  <samshen@wondermedia.com.tw>
 *     * Add License declaration and ChangeLog
 */


/*----------------------- DEPENDENCE -----------------------------------------*/
#include "vpp.h"
#include "wmt_display.h"
/*----------------------- Backlight --------------------------------------*/
#ifdef CONFIG_VPP_SHENZHEN
#include "../../board/wmt/include/wmt_iomux.h"

#define ENV_LOGO_LED  "wmt.logo.led"

static void logo_led_power_on(int on);
#endif

#define pwm_write_reg(addr,val,wait)	\
	REG32_VAL(addr) = val;	\
	while(REG32_VAL(0xd8220040)&=wait);

#define UBOOT_PWM_INVERT 0x10
void lcd_blt_enable(int no,int enable)
{
#ifdef CONFIG_VPP_SHENZHEN
	lcd_backlight_power_on(enable != 0);
#endif
	pwm_set_enable(no,enable);

#ifdef CONFIG_VPP_SHENZHEN
    logo_led_power_on(enable);
#endif

} /* End of vt8430_blt_enable */

void lcd_blt_set_level(int no,int level)
{
	return;
}

void lcd_blt_set_freq(int no,unsigned int freq)
{
	return;
}

void lcd_blt_set_pwm(int no, int level, int freq)
{
	int clock = auto_pll_divisor(DEV_PWM,GET_FREQ, 0, 0);
	int period, duty, scalar;

	clock = clock / freq;
	scalar = 0;
	period = 2000;

	while(period > 1023) {
		scalar++;
		period = clock / scalar;
	}

	duty = (period*level)/100;
	duty = (duty)? (duty-1):0;
	scalar = scalar-1;
	period = period -1;

	pwm_set_period(no,period);
	pwm_set_duty(no,duty);
	pwm_set_scalar(no,scalar);
	if ((g_pwm_setting.config&UBOOT_PWM_INVERT) != 0)
		pwm_set_control(no,(level)? 0x37:0x8);
	else
		pwm_set_control(no,(level)? 0x35:0x8);
	pwm_set_gpio(no,level);
}

void pwm_set_enable(int no,int enable)
{
	unsigned int addr,reg,reg1;

	addr = PWM_CTRL_REG_ADDR + (0x10 * no);
	reg = REG32_VAL(addr);
	if( enable ){
		reg |= PWM_ENABLE;
	}
	else {
		reg &= ~PWM_ENABLE;
	}

	pwm_write_reg(addr,reg,PWM_CTRL_UPDATE << (4*no));
	reg1 = REG32_VAL(addr);
	pwm_set_gpio(no,enable);
}

void pwm_set_control(int no,unsigned int ctrl)
{
	unsigned int addr;

	addr = PWM_CTRL_REG_ADDR + (0x10 * no);
	pwm_write_reg(addr,ctrl,PWM_CTRL_UPDATE << (8*no));
} /* End of pwm_proc */

void pwm_set_scalar(int no,unsigned int scalar)
{
	unsigned int addr;

	addr = PWM_SCALAR_REG_ADDR + (0x10 * no);
	pwm_write_reg(addr,scalar,PWM_SCALAR_UPDATE << (8*no));
}

void pwm_set_period(int no,unsigned int period)
{
	unsigned int addr;

	addr = PWM_PERIOD_REG_ADDR + (0x10 * no);
	pwm_write_reg(addr,period,PWM_PERIOD_UPDATE << (8*no));
}

void pwm_set_duty(int no,unsigned int duty)
{
	unsigned int addr;

	addr = PWM_DUTY_REG_ADDR + (0x10 * no);
	pwm_write_reg(addr,duty,PWM_DUTY_UPDATE << (8*no));
}

unsigned int pwm_get_period(int no)
{
	unsigned int addr;

	addr = PWM_PERIOD_REG_ADDR + (0x10 * no);
	return (REG32_VAL(addr) & 0xFFF);
}

unsigned int pwm_get_duty(int no)
{
	unsigned int addr;

	addr = PWM_DUTY_REG_ADDR + (0x10 * no);
	return (REG32_VAL(addr) & 0xFFF);
}

unsigned int pwm_get_enable(int no)
{
	unsigned int addr,reg;

	addr = PWM_CTRL_REG_ADDR + (0x10 * no);
	reg = REG32_VAL(addr);
	reg &= PWM_ENABLE;
	return reg;
}

void set_lcd_power(int on)
{
	unsigned int val;

	if ((g_lcd_pw_pin.ctl == 0) || (g_lcd_pw_pin.oc == 0) || (g_lcd_pw_pin.od == 0)) {
		printf("lcd power ping not define\n");
		return;
	}
	val = 1<<g_lcd_pw_pin.bitmap;
	if (g_lcd_pw_pin.act == 0)
		on = ~on;
	REG32_VAL(g_lcd_pw_pin.ctl) |= val;
	REG32_VAL(g_lcd_pw_pin.oc) |= val;
	if (on) {
		REG32_VAL(g_lcd_pw_pin.od) |= val;
	} else {
		REG32_VAL(g_lcd_pw_pin.od) &= ~val;
	}
}

void pwm_set_gpio(int no,int enable)
{
	/* unsigned int pwm_pin; */

	if( enable ) {
		if((g_pwm_setting.config&UBOOT_PWM_INVERT) != 0)
		REG8_VAL(GPIO_OD_GP20_PWM0_BYTE_ADDR) &= 0xFE;
		else
			REG8_VAL(GPIO_OD_GP20_PWM0_BYTE_ADDR) |= 0x01;

		REG8_VAL(GPIO_OC_GP20_PWM0_BYTE_ADDR) &= 0xFE;
		REG8_VAL(GPIO_CTRL_GP20_PWM0_BYTE_ADDR) &= 0xFE;
	} else {
		if((g_pwm_setting.config&UBOOT_PWM_INVERT) != 0)
			REG8_VAL(GPIO_OD_GP20_PWM0_BYTE_ADDR) |= 0x01;
		else
		REG8_VAL(GPIO_OD_GP20_PWM0_BYTE_ADDR) &= 0xFE;

		REG8_VAL(GPIO_OC_GP20_PWM0_BYTE_ADDR) |= 0x01;
		REG8_VAL(GPIO_CTRL_GP20_PWM0_BYTE_ADDR) |= 0x01;
	}
	/* set to PWM mode */
	if ( no == 0 )
		REG32_VAL(PIN_SHARING_SEL_4BYTE_ADDR) &= ~0x1000;
	else if( no == 1 )
		REG32_VAL(PIN_SHARING_SEL_4BYTE_ADDR) &= ~0x80;
	/* do not control back-light power in pwm */
	/*
	pwm_pin = REG32_VAL(PWM_CTRL_REG_ADDR + (0x10 * no));
	if (((pwm_pin&PWM_ENABLE) != 0)&&(enable == 1))
		set_lcd_power(1);
	if (((pwm_pin&PWM_ENABLE) == 0)&&(enable == 0))
		set_lcd_power(0);
	*/
}

#ifdef CONFIG_VPP_SHENZHEN

static void logo_led_set_pwm(int no, int level, int freq, int active, int init)
{
	int clock;
	int period, duty, scalar;

	if(init) {
		clock = auto_pll_divisor(DEV_PWM,GET_FREQ, 0, 0);
		clock = clock / freq;
		scalar = 0;
		period = 2000;

		while(period > 1023) {
			scalar++;
			period = clock / scalar;
		}

		duty = (period*level)/100;
		duty = (duty)? (duty-1):0;
		scalar = scalar-1;
		period = period -1;

		pwm_set_period(no,period);
		pwm_set_duty(no,duty);
		pwm_set_scalar(no,scalar);
		REG32_VAL(PIN_SHARING_SEL_4BYTE_ADDR) &= ~0x80;
	}

	if (active == 0)
		pwm_set_control(no,(level)? 0x37:0x8);
	else
		pwm_set_control(no,(level)? 0x35:0x8);
}

#define PWM2_GPIO_NO  200

static void logo_led_power_on(int on)
{
	int ret;
	static int led_init;
	static struct gpio_param_t led_control_pin;

	if(led_init == -1)
		return;

	if(led_init == 0) {
		ret = parse_gpio_param(ENV_LOGO_LED, &led_control_pin);
		if(ret) {
			led_init = -1;
			return;
		}

		led_init = 1;
	}

	switch (led_control_pin.gpiono) {
		case PWM2_GPIO_NO:
			if(on)
				logo_led_set_pwm(2, 100, 220000, led_control_pin.act, 1);
			else
				logo_led_set_pwm(2, 100, 220000, !led_control_pin.act, 1);
		break;

		default:
			if(on)
				gpio_direction_output(led_control_pin.gpiono, led_control_pin.act);
			else
				gpio_direction_output(led_control_pin.gpiono, !led_control_pin.act);
		break;
	}
}
#endif