import PULS_20180308
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / misc / mediatek / leds / mt8127 / leds.c
1 /*
2 * drivers/leds/leds-mt65xx.c
3 *
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file COPYING in the main directory of this archive for
6 * more details.
7 *
8 * mt65xx leds driver
9 *
10 */
11
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/delay.h>
15 #include <linux/string.h>
16 #include <linux/ctype.h>
17 #include <linux/leds.h>
18 #include <linux/leds-mt65xx.h>
19 #include <linux/workqueue.h>
20 #include <linux/wakelock.h>
21 #include <linux/slab.h>
22 //#include <cust_leds.h>
23 //#include <cust_leds_def.h>
24 #include <mach/mt_pwm.h>
25 #include <mach/mt_boot.h>
26 //#include <mach/mt_gpio.h>
27 #include <mach/pmic_mt6329_hw_bank1.h>
28 #include <mach/pmic_mt6329_sw_bank1.h>
29 #include <mach/pmic_mt6329_hw.h>
30 #include <mach/pmic_mt6329_sw.h>
31 #include <mach/upmu_common_sw.h>
32 #include <mach/upmu_hw.h>
33 #include "leds_sw.h"
34 //#include <linux/leds_sw.h>
35 //#include <mach/mt_pmic_feature_api.h>
36 //#include <mach/mt_boot.h>
37 #include <linux/aal_api.h>
38 #include <linux/aee.h>
39
40
41 static DEFINE_MUTEX(leds_mutex);
42 static DEFINE_MUTEX(leds_pmic_mutex);
43 //#define ISINK_CHOP_CLK
44 /****************************************************************************
45 * variables
46 ***************************************************************************/
47 //struct cust_mt65xx_led* bl_setting_hal = NULL;
48
49 /*[PLATFORM]-ADD-BEIGIN by falin.luo 2015/6/15*/
50 /*hall sensor use bl_brightness_hal to detect system status*/
51 #ifdef CONFIG_MTK_HALL
52 unsigned int bl_brightness_hal = 102;
53 #else
54 static unsigned int bl_brightness_hal = 102;
55 #endif
56 /*[PLATFORM]-ADD-END by falin.luo 2015/6/15*/
57 static unsigned int bl_duty_hal = 21;
58 static unsigned int bl_div_hal = CLK_DIV1;
59 static unsigned int bl_frequency_hal = 32000;
60 //for button led don't do ISINK disable first time
61 static int button_flag_isink0 = 0;
62 static int button_flag_isink1 = 0;
63 static int button_flag_isink2 = 0;
64 static int button_flag_isink3 = 0;
65 /*************************************************************/
66 struct wake_lock leds_suspend_lock;
67
68 /****************************************************************************
69 * DEBUG MACROS
70 ***************************************************************************/
71 static int debug_enable_led_hal = 1;
72 #define LEDS_DEBUG(format, args...) do{ \
73 if(debug_enable_led_hal) \
74 {\
75 printk(KERN_WARNING format,##args);\
76 }\
77 }while(0)
78
79 /****************************************************************************
80 * custom APIs
81 ***************************************************************************/
82 extern unsigned int brightness_mapping(unsigned int level);
83
84 /*****************PWM *************************************************/
85 static int time_array_hal[PWM_DIV_NUM]={256,512,1024,2048,4096,8192,16384,32768};
86 static unsigned int div_array_hal[PWM_DIV_NUM] = {1,2,4,8,16,32,64,128};
87 static unsigned int backlight_PWM_div_hal = CLK_DIV1;// this para come from cust_leds.
88
89 /******************************************************************************
90 for DISP backlight High resolution
91 ******************************************************************************/
92 #ifdef LED_INCREASE_LED_LEVEL_MTKPATCH
93 #define MT_LED_INTERNAL_LEVEL_BIT_CNT 10
94 #endif
95
96 // Use Old Mode of PWM to suppoort 256 backlight level
97 #define BACKLIGHT_LEVEL_PWM_256_SUPPORT 256
98 extern unsigned int Cust_GetBacklightLevelSupport_byPWM(void);
99
100 /****************************************************************************
101 * func:return global variables
102 ***************************************************************************/
103
104 void mt_leds_wake_lock_init(void)
105 {
106 wake_lock_init(&leds_suspend_lock, WAKE_LOCK_SUSPEND, "leds wakelock");
107 }
108
109 unsigned int mt_get_bl_brightness(void)
110 {
111 return bl_brightness_hal;
112 }
113
114 unsigned int mt_get_bl_duty(void)
115 {
116 return bl_duty_hal;
117 }
118 unsigned int mt_get_bl_div(void)
119 {
120 return bl_div_hal;
121 }
122 unsigned int mt_get_bl_frequency(void)
123 {
124 return bl_frequency_hal;
125 }
126
127 unsigned int *mt_get_div_array(void)
128 {
129 return &div_array_hal[0];
130 }
131
132 void mt_set_bl_duty(unsigned int level)
133 {
134 bl_duty_hal = level;
135 }
136
137 void mt_set_bl_div(unsigned int div)
138 {
139 bl_div_hal = div;
140 }
141
142 void mt_set_bl_frequency(unsigned int freq)
143 {
144 bl_frequency_hal = freq;
145 }
146
147 struct cust_mt65xx_led * mt_get_cust_led_list(void)
148 {
149 return get_cust_led_list();
150 }
151
152
153 /****************************************************************************
154 * internal functions
155 ***************************************************************************/
156 //#if 0
157 static int brightness_mapto64(int level)
158 {
159 if (level < 30)
160 return (level >> 1) + 7;
161 else if (level <= 120)
162 return (level >> 2) + 14;
163 else if (level <= 160)
164 return level / 5 + 20;
165 else
166 return (level >> 3) + 33;
167 }
168
169
170
171 static int find_time_index(int time)
172 {
173 int index = 0;
174 while(index < 8)
175 {
176 if(time<time_array_hal[index])
177 return index;
178 else
179 index++;
180 }
181 return PWM_DIV_NUM-1;
182 }
183
184 int mt_led_set_pwm(int pwm_num, struct nled_setting* led)
185 {
186 //struct pwm_easy_config pwm_setting;
187 struct pwm_spec_config pwm_setting;
188 int time_index = 0;
189 pwm_setting.pwm_no = pwm_num;
190 pwm_setting.mode = PWM_MODE_OLD;
191
192 LEDS_DEBUG("[LED]led_set_pwm: mode=%d,pwm_no=%d\n", led->nled_mode, pwm_num);
193 //if((pwm_num != PWM3 && pwm_num != PWM4 && pwm_num != PWM5))//AP PWM all support OLD mode in MT6589
194 pwm_setting.clk_src = PWM_CLK_OLD_MODE_32K;
195 //else
196 //pwm_setting.clk_src = PWM_CLK_NEW_MODE_BLOCK_DIV_BY_1625;
197
198 switch (led->nled_mode)
199 {
200 case NLED_OFF :
201 pwm_setting.PWM_MODE_OLD_REGS.THRESH = 0;
202 pwm_setting.clk_div = CLK_DIV1;
203 pwm_setting.PWM_MODE_OLD_REGS.DATA_WIDTH = 100;
204 break;
205
206 case NLED_ON :
207 pwm_setting.PWM_MODE_OLD_REGS.THRESH = 30;
208 pwm_setting.clk_div = CLK_DIV1;
209 pwm_setting.PWM_MODE_OLD_REGS.DATA_WIDTH = 100;
210 break;
211
212 case NLED_BLINK :
213 LEDS_DEBUG("[LED]LED blink on time = %d offtime = %d\n",led->blink_on_time,led->blink_off_time);
214 time_index = find_time_index(led->blink_on_time + led->blink_off_time);
215 LEDS_DEBUG("[LED]LED div is %d\n",time_index);
216 pwm_setting.clk_div = time_index;
217 pwm_setting.PWM_MODE_OLD_REGS.DATA_WIDTH = (led->blink_on_time + led->blink_off_time) * MIN_FRE_OLD_PWM / div_array_hal[time_index];
218 pwm_setting.PWM_MODE_OLD_REGS.THRESH = (led->blink_on_time*100) / (led->blink_on_time + led->blink_off_time);
219 }
220
221 pwm_setting.PWM_MODE_FIFO_REGS.IDLE_VALUE = 0;
222 pwm_setting.PWM_MODE_FIFO_REGS.GUARD_VALUE = 0;
223 pwm_setting.PWM_MODE_FIFO_REGS.GDURATION = 0;
224 pwm_setting.PWM_MODE_FIFO_REGS.WAVE_NUM = 0;
225 pwm_set_spec_config(&pwm_setting);
226
227 return 0;
228
229 }
230
231
232 //***********************MT6589 led breath funcion*******************************//
233 #if 0
234 static int led_breath_pmic(enum mt65xx_led_pmic pmic_type, struct nled_setting* led)
235 {
236 //int time_index = 0;
237 //int duty = 0;
238 LEDS_DEBUG("[LED]led_blink_pmic: pmic_type=%d\n", pmic_type);
239
240 if((pmic_type != MT65XX_LED_PMIC_NLED_ISINK0 && pmic_type!= MT65XX_LED_PMIC_NLED_ISINK1) || led->nled_mode != NLED_BLINK) {
241 return -1;
242 }
243
244 switch(pmic_type){
245 case MT65XX_LED_PMIC_NLED_ISINK0://keypad center 4mA
246 upmu_set_isinks_ch0_mode(PMIC_PWM_0);// 6320 spec
247 upmu_set_isinks_ch0_step(0x0);//4mA
248 upmu_set_isinks_breath0_trf_sel(0x04);
249 upmu_set_isinks_breath0_ton_sel(0x02);
250 upmu_set_isinks_breath0_toff_sel(0x03);
251 upmu_set_isink_dim1_duty(15);
252 upmu_set_isink_dim1_fsel(11);//6320 0.25KHz
253 upmu_set_rg_spk_div_pdn(0x01);
254 upmu_set_rg_spk_pwm_div_pdn(0x01);
255 upmu_set_isinks_ch0_en(0x01);
256 break;
257 case MT65XX_LED_PMIC_NLED_ISINK1://keypad LR 16mA
258 upmu_set_isinks_ch1_mode(PMIC_PWM_1);// 6320 spec
259 upmu_set_isinks_ch1_step(0x3);//16mA
260 upmu_set_isinks_breath0_trf_sel(0x04);
261 upmu_set_isinks_breath0_ton_sel(0x02);
262 upmu_set_isinks_breath0_toff_sel(0x03);
263 upmu_set_isink_dim1_duty(15);
264 upmu_set_isink_dim1_fsel(11);//6320 0.25KHz
265 upmu_set_rg_spk_div_pdn(0x01);
266 upmu_set_rg_spk_pwm_div_pdn(0x01);
267 upmu_set_isinks_ch1_en(0x01);
268 break;
269 default:
270 break;
271 }
272 return 0;
273
274 }
275
276 #endif
277
278
279 #ifdef CONFIG_MTK_PMIC_MT6397
280
281 #define PMIC_PERIOD_NUM 13
282 //int pmic_period_array[] = {250,500,1000,1250,1666,2000,2500,3333,4000,5000,6666,8000,10000,13333,16000};
283 int pmic_period_array[] = {250,500,1000,1250,1666,2000,2500,3333,4000,5000,6666,8000,10000};
284 u8 pmic_clksel_array[] = {0,0,0,0,0,0,1,1,1,2,2,2,3,3,3};
285 //u8 pmic_freqsel_array[] = {24,26,28,29,30,31,29,30,31,29,30,31,29,30,31};
286 //u8 pmic_freqsel_array[] = {21,22,23,24,25,26,24,25,26,24,25,26,24,25,26};
287 u8 pmic_freqsel_array[] = {21,22,23,24,24,24,25,26,26,26,28,28,28};
288
289 #else
290
291 #define PMIC_PERIOD_NUM 9
292 // 100 * period, ex: 0.01 Hz -> 0.01 * 100 = 1
293 int pmic_period_array[] = {250,500,1000,1250,1666,2000,2500,10000};
294 //int pmic_freqsel_array[] = {99999, 9999, 4999, 1999, 999, 499, 199, 4, 0};
295 int pmic_freqsel_array[] = {0, 4, 199, 499, 999, 1999, 1999, 1999};
296
297 #endif
298
299 static int find_time_index_pmic(int time_ms) {
300 int i;
301 for(i=0;i<PMIC_PERIOD_NUM;i++) {
302 if(time_ms<=pmic_period_array[i]) {
303 return i;
304 } else {
305 continue;
306 }
307 }
308 return PMIC_PERIOD_NUM-1;
309 }
310
311 #ifdef CONFIG_MTK_PMIC_MT6397
312 int mt_led_blink_pmic(enum mt65xx_led_pmic pmic_type, struct nled_setting* led) {
313 int time_index = 0;
314 int duty = 0;
315 LEDS_DEBUG("[LED]led_blink_pmic: pmic_type=%d\n", pmic_type);
316
317 if((pmic_type != MT65XX_LED_PMIC_NLED_ISINK0 && pmic_type!= MT65XX_LED_PMIC_NLED_ISINK1 && pmic_type!= MT65XX_LED_PMIC_NLED_ISINK2) || led->nled_mode != NLED_BLINK) {
318 return -1;
319 }
320
321 LEDS_DEBUG("[LED]LED blink on time = %d offtime = %d\n",led->blink_on_time,led->blink_off_time);
322 time_index = find_time_index_pmic(led->blink_on_time + led->blink_off_time);
323 LEDS_DEBUG("[LED]LED index is %d clksel=%d freqsel=%d\n", time_index, pmic_clksel_array[time_index], pmic_freqsel_array[time_index]);
324 duty=32*led->blink_on_time/(led->blink_on_time + led->blink_off_time);
325 switch(pmic_type){
326 case MT65XX_LED_PMIC_NLED_ISINK0://keypad center 4mA
327 upmu_set_rg_bst_drv_1m_ck_pdn(0x0);
328 upmu_set_isinks_ch0_mode(PMIC_PWM_0);// 6320 spec
329 upmu_set_isinks_ch0_step(0x0);//4mA
330 upmu_set_isink_dim0_duty(duty);
331 upmu_set_isink_dim0_fsel(pmic_freqsel_array[time_index]);
332 //upmu_set_rg_spk_pwm_div_pdn(0x01);
333 //upmu_set_rg_bst_drv_1m_ck_pdn(0x0);
334 #ifdef ISINK_CHOP_CLK
335 //upmu_set_isinks0_chop_en(0x1);
336 #endif
337 upmu_set_isinks_breath0_trf_sel(0x0);
338 upmu_set_isinks_breath0_ton_sel(0x02);
339 upmu_set_isinks_breath0_toff_sel(0x05);
340
341 upmu_set_isinks_ch0_en(0x01);
342 break;
343 case MT65XX_LED_PMIC_NLED_ISINK1://keypad LR 16mA
344 upmu_set_rg_bst_drv_1m_ck_pdn(0x0);
345 upmu_set_isinks_ch1_mode(PMIC_PWM_0);// 6320 spec
346 upmu_set_isinks_ch1_step(0x3);//16mA
347 upmu_set_isink_dim1_duty(duty);
348 upmu_set_isink_dim1_fsel(pmic_freqsel_array[time_index]);
349 upmu_set_isinks_breath1_trf_sel(0x0);
350 upmu_set_isinks_breath1_ton_sel(0x02);
351 upmu_set_isinks_breath1_toff_sel(0x05);
352 //upmu_set_rg_spk_pwm_div_pdn(0x01);
353
354 #ifdef ISINK_CHOP_CLK
355 //upmu_set_isinks1_chop_en(0x1);
356 #endif
357 upmu_set_isinks_ch1_en(0x01);
358 break;
359 case MT65XX_LED_PMIC_NLED_ISINK2://keypad LR 16mA
360 upmu_set_rg_bst_drv_1m_ck_pdn(0x0);
361 upmu_set_isinks_ch2_mode(PMIC_PWM_0);// 6320 spec
362 upmu_set_isinks_ch2_step(0x3);//16mA
363 upmu_set_isink_dim2_duty(duty);
364 upmu_set_isink_dim2_fsel(pmic_freqsel_array[time_index]);
365 //upmu_set_rg_spk_pwm_div_pdn(0x01);
366 upmu_set_isinks_breath2_trf_sel(0x0);
367 upmu_set_isinks_breath2_ton_sel(0x02);
368 upmu_set_isinks_breath2_toff_sel(0x05);
369 #ifdef ISINK_CHOP_CLK
370 //upmu_set_isinks2_chop_en(0x1);
371 #endif
372 upmu_set_isinks_ch2_en(0x01);
373 break;
374 default:
375 break;
376 }
377 return 0;
378 }
379 #else
380 int mt_led_blink_pmic(enum mt65xx_led_pmic pmic_type, struct nled_setting* led) {
381 int time_index = 0;
382 int duty = 0;
383 LEDS_DEBUG("[LED]led_blink_pmic: pmic_type=%d\n", pmic_type);
384
385 if((pmic_type != MT65XX_LED_PMIC_NLED_ISINK0 && pmic_type!= MT65XX_LED_PMIC_NLED_ISINK1 &&
386 pmic_type!= MT65XX_LED_PMIC_NLED_ISINK2 && pmic_type!= MT65XX_LED_PMIC_NLED_ISINK3) || led->nled_mode != NLED_BLINK) {
387 return -1;
388 }
389
390 LEDS_DEBUG("[LED]LED blink on time = %d offtime = %d\n",led->blink_on_time,led->blink_off_time);
391 time_index = find_time_index_pmic(led->blink_on_time + led->blink_off_time);
392 LEDS_DEBUG("[LED]LED index is %d freqsel=%d\n", time_index, pmic_freqsel_array[time_index]);
393 duty=32*led->blink_on_time/(led->blink_on_time + led->blink_off_time);
394 //upmu_set_rg_drv_2m_ck_pdn(0x0); // Disable power down (Indicator no need)
395 upmu_set_rg_drv_32k_ck_pdn(0x0); // Disable power down
396 switch(pmic_type){
397 case MT65XX_LED_PMIC_NLED_ISINK0:
398 upmu_set_rg_isink0_ck_pdn(0);
399 upmu_set_rg_isink0_ck_sel(0);
400 upmu_set_isink_ch0_mode(PMIC_PWM_0);
401 upmu_set_isink_ch0_step(0x3);//16mA
402 upmu_set_isink_dim0_duty(duty);
403 upmu_set_isink_dim0_fsel(pmic_freqsel_array[time_index]);
404 upmu_set_isink_breath0_trf_sel(0x0);
405 upmu_set_isink_breath0_ton_sel(0x02);
406 upmu_set_isink_breath0_toff_sel(0x05);
407 upmu_set_isink_ch0_en(0x01);
408
409 break;
410 case MT65XX_LED_PMIC_NLED_ISINK1:
411 upmu_set_rg_isink1_ck_pdn(0);
412 upmu_set_rg_isink1_ck_sel(0);
413 upmu_set_isink_ch1_mode(PMIC_PWM_0);
414 upmu_set_isink_ch1_step(0x3);//16mA
415 upmu_set_isink_dim1_duty(duty);
416 upmu_set_isink_dim1_fsel(pmic_freqsel_array[time_index]);
417 upmu_set_isink_breath1_trf_sel(0x0);
418 upmu_set_isink_breath1_ton_sel(0x02);
419 upmu_set_isink_breath1_toff_sel(0x05);
420 upmu_set_isink_ch1_en(0x01);
421
422 break;
423 case MT65XX_LED_PMIC_NLED_ISINK2:
424 upmu_set_rg_isink2_ck_pdn(0);
425 upmu_set_rg_isink2_ck_sel(0);
426 upmu_set_isink_ch2_mode(PMIC_PWM_0);
427 upmu_set_isink_ch2_step(0x3);//16mA
428 upmu_set_isink_dim2_duty(duty);
429 upmu_set_isink_dim2_fsel(pmic_freqsel_array[time_index]);
430 upmu_set_isink_breath2_trf_sel(0x0);
431 upmu_set_isink_breath2_ton_sel(0x02);
432 upmu_set_isink_breath2_toff_sel(0x05);
433 upmu_set_isink_ch2_en(0x01);
434
435 break;
436 case MT65XX_LED_PMIC_NLED_ISINK3:
437 upmu_set_rg_isink3_ck_pdn(0);
438 upmu_set_rg_isink3_ck_sel(0);
439 upmu_set_isink_ch3_mode(PMIC_PWM_0);
440 upmu_set_isink_ch3_step(0x3);//16mA
441 upmu_set_isink_dim3_duty(duty);
442 upmu_set_isink_dim3_fsel(pmic_freqsel_array[time_index]);
443 upmu_set_isink_breath3_trf_sel(0x0);
444 upmu_set_isink_breath3_ton_sel(0x02);
445 upmu_set_isink_breath3_toff_sel(0x05);
446 upmu_set_isink_ch3_en(0x01);
447
448 break;
449 default:
450 break;
451 }
452 return 0;
453 }
454 #endif
455
456 int mt_backlight_set_pwm(int pwm_num, u32 level, u32 div, struct PWM_config *config_data)
457 {
458 struct pwm_spec_config pwm_setting;
459 unsigned int BacklightLevelSupport = Cust_GetBacklightLevelSupport_byPWM();
460 pwm_setting.pwm_no = pwm_num;
461
462 if (BacklightLevelSupport == BACKLIGHT_LEVEL_PWM_256_SUPPORT)
463 pwm_setting.mode = PWM_MODE_OLD;
464 else
465 pwm_setting.mode = PWM_MODE_FIFO; //new mode fifo and periodical mode
466
467 pwm_setting.pmic_pad = config_data->pmic_pad;
468
469 if(config_data->div)
470 {
471 pwm_setting.clk_div = config_data->div;
472 backlight_PWM_div_hal = config_data->div;
473 }
474 else
475 pwm_setting.clk_div = div;
476
477 if(BacklightLevelSupport== BACKLIGHT_LEVEL_PWM_256_SUPPORT)
478 {
479 if(config_data->clock_source)
480 {
481 pwm_setting.clk_src = PWM_CLK_OLD_MODE_BLOCK;
482 }
483 else
484 {
485 pwm_setting.clk_src = PWM_CLK_OLD_MODE_32K; // actually. it's block/1625 = 26M/1625 = 16KHz @ MT6571
486 }
487
488 pwm_setting.PWM_MODE_OLD_REGS.IDLE_VALUE = 0;
489 pwm_setting.PWM_MODE_OLD_REGS.GUARD_VALUE = 0;
490 pwm_setting.PWM_MODE_OLD_REGS.GDURATION = 0;
491 pwm_setting.PWM_MODE_OLD_REGS.WAVE_NUM = 0;
492 pwm_setting.PWM_MODE_OLD_REGS.DATA_WIDTH = 255; // 256 level
493 pwm_setting.PWM_MODE_OLD_REGS.THRESH = level;
494
495 LEDS_DEBUG("[LEDS][%d]backlight_set_pwm:duty is %d/%d\n", BacklightLevelSupport, level, pwm_setting.PWM_MODE_OLD_REGS.DATA_WIDTH);
496 LEDS_DEBUG("[LEDS][%d]backlight_set_pwm:clk_src/div is %d%d\n", BacklightLevelSupport, pwm_setting.clk_src, pwm_setting.clk_div);
497 if(level >0 && level < 256)
498 {
499 pwm_set_spec_config(&pwm_setting);
500 LEDS_DEBUG("[LEDS][%d]backlight_set_pwm: old mode: thres/data_width is %d/%d\n", BacklightLevelSupport, pwm_setting.PWM_MODE_OLD_REGS.THRESH, pwm_setting.PWM_MODE_OLD_REGS.DATA_WIDTH);
501 }
502 else
503 {
504 LEDS_DEBUG("[LEDS][%d]Error level in backlight\n", BacklightLevelSupport);
505 mt_pwm_disable(pwm_setting.pwm_no, config_data->pmic_pad);
506 }
507 return 0;
508
509 }
510 else
511 {
512 if(config_data->clock_source)
513 {
514 pwm_setting.clk_src = PWM_CLK_NEW_MODE_BLOCK;
515 }
516 else
517 {
518 pwm_setting.clk_src = PWM_CLK_NEW_MODE_BLOCK_DIV_BY_1625;
519 }
520
521 if(config_data->High_duration && config_data->low_duration)
522 {
523 pwm_setting.PWM_MODE_FIFO_REGS.HDURATION = config_data->High_duration;
524 pwm_setting.PWM_MODE_FIFO_REGS.LDURATION = pwm_setting.PWM_MODE_FIFO_REGS.HDURATION;
525 }
526 else
527 {
528 pwm_setting.PWM_MODE_FIFO_REGS.HDURATION = 4;
529 pwm_setting.PWM_MODE_FIFO_REGS.LDURATION = 4;
530 }
531 pwm_setting.PWM_MODE_FIFO_REGS.IDLE_VALUE = 0;
532 pwm_setting.PWM_MODE_FIFO_REGS.GUARD_VALUE = 0;
533 pwm_setting.PWM_MODE_FIFO_REGS.STOP_BITPOS_VALUE = 31;
534 pwm_setting.PWM_MODE_FIFO_REGS.GDURATION = (pwm_setting.PWM_MODE_FIFO_REGS.HDURATION+1)*32 - 1;
535 pwm_setting.PWM_MODE_FIFO_REGS.WAVE_NUM = 0;
536
537 LEDS_DEBUG("[LED]backlight_set_pwm:duty is %d\n", level);
538 LEDS_DEBUG("[LED]backlight_set_pwm:clk_src/div/high/low is %d%d%d%d\n", pwm_setting.clk_src,pwm_setting.clk_div,pwm_setting.PWM_MODE_FIFO_REGS.HDURATION,pwm_setting.PWM_MODE_FIFO_REGS.LDURATION);
539 if(level>0 && level <= 32)
540 {
541 pwm_setting.PWM_MODE_FIFO_REGS.GUARD_VALUE = 0;
542 pwm_setting.PWM_MODE_FIFO_REGS.SEND_DATA0 = (1 << level) - 1 ;
543 //pwm_setting.PWM_MODE_FIFO_REGS.SEND_DATA1 = 0 ;
544 pwm_set_spec_config(&pwm_setting);
545 }else if(level>32 && level <=64)
546 {
547 pwm_setting.PWM_MODE_FIFO_REGS.GUARD_VALUE = 1;
548 level -= 32;
549 pwm_setting.PWM_MODE_FIFO_REGS.SEND_DATA0 = (1 << level) - 1 ;
550 //pwm_setting.PWM_MODE_FIFO_REGS.SEND_DATA0 = 0xFFFFFFFF ;
551 //pwm_setting.PWM_MODE_FIFO_REGS.SEND_DATA1 = (1 << level) - 1;
552 pwm_set_spec_config(&pwm_setting);
553 }else
554 {
555 LEDS_DEBUG("[LED]Error level in backlight\n");
556 //mt_set_pwm_disable(pwm_setting.pwm_no);
557 //mt_pwm_power_off(pwm_setting.pwm_no);
558 mt_pwm_disable(pwm_setting.pwm_no, config_data->pmic_pad);
559 }
560 //printk("[LED]PWM con register is %x \n", INREG32(PWM_BASE + 0x0150));
561 return 0;
562 }
563 }
564
565 void mt_led_pwm_disable(int pwm_num)
566 {
567 struct cust_mt65xx_led *cust_led_list = get_cust_led_list();
568 mt_pwm_disable(pwm_num, cust_led_list->config_data.pmic_pad);
569 }
570
571 void mt_backlight_set_pwm_duty(int pwm_num, u32 level, u32 div, struct PWM_config *config_data)
572 {
573 mt_backlight_set_pwm(pwm_num, level, div, config_data);
574 }
575
576 void mt_backlight_set_pwm_div(int pwm_num, u32 level, u32 div, struct PWM_config *config_data)
577 {
578 mt_backlight_set_pwm(pwm_num, level, div, config_data);
579 }
580
581 void mt_backlight_get_pwm_fsel(unsigned int bl_div, unsigned int *bl_frequency)
582 {
583
584 }
585
586 void mt_store_pwm_register(unsigned int addr, unsigned int value)
587 {
588
589 }
590
591 unsigned int mt_show_pwm_register(unsigned int addr)
592 {
593 return 0;
594 }
595 #if 0
596 static void pmic_isink_power_set(int enable)
597 {
598 int i = 0;
599 struct cust_mt65xx_led *cust_led_list = get_cust_led_list();
600 if(enable) {
601 for (i = 0; i < MT65XX_LED_TYPE_TOTAL; i++) {
602 if ((cust_led_list[i].mode == MT65XX_LED_MODE_PMIC) && (cust_led_list[i].data == MT65XX_LED_PMIC_LCD_ISINK0 ||
603 cust_led_list[i].data == MT65XX_LED_PMIC_LCD_ISINK1 || cust_led_list[i].data == MT65XX_LED_PMIC_LCD_ISINK2 ||
604 cust_led_list[i].data == MT65XX_LED_PMIC_LCD_ISINK3)) {
605 upmu_set_rg_drv_2m_ck_pdn(0);
606 break;
607 }
608 }
609 for (i = 0; i < MT65XX_LED_TYPE_TOTAL; i++) {
610 if ((cust_led_list[i].mode == MT65XX_LED_MODE_PMIC) && (cust_led_list[i].data == MT65XX_LED_PMIC_NLED_ISINK0 ||
611 cust_led_list[i].data == MT65XX_LED_PMIC_NLED_ISINK1 || cust_led_list[i].data == MT65XX_LED_PMIC_NLED_ISINK2 ||
612 cust_led_list[i].data == MT65XX_LED_PMIC_NLED_ISINK3)) {
613 upmu_set_rg_drv_32k_ck_pdn(0);
614 break;
615 }
616 }
617 }else {
618 upmu_set_rg_drv_2m_ck_pdn(1);
619 upmu_set_rg_drv_32k_ck_pdn(1);
620 }
621 }
622 // define for 15 level mapping(backlight controlled by PMIC ISINK channel)
623
624 static void brightness_set_pmic_isink_duty(unsigned int level)
625 {
626 if(level<11) {
627 upmu_set_isink_dim0_duty(level);
628 }else {
629 switch(level) {
630 case 11:
631 upmu_set_isink_dim0_duty(12);
632 break;
633 case 12:
634 upmu_set_isink_dim0_duty(16);
635 break;
636 case 13:
637 upmu_set_isink_dim0_duty(20);
638 break;
639 case 14:
640 upmu_set_isink_dim0_duty(25);
641 break;
642 case 15:
643 upmu_set_isink_dim0_duty(31);
644 break;
645 default:
646 LEDS_DEBUG("mapping BLK level is error for ISINK!\n");
647 break;
648 }
649 }
650 }
651 #endif
652 #ifdef CONFIG_MTK_PMIC_MT6397
653 int mt_brightness_set_pmic(enum mt65xx_led_pmic pmic_type, u32 level, u32 div)
654 {
655 //int tmp_level = level;
656 //static bool backlight_init_flag[2] = {false, false};
657 static bool led_init_flag[3] = {false, false, false};
658 static bool first_time = true;
659
660 LEDS_DEBUG("[LED]PMIC#%d:%d\n", pmic_type, level);
661
662 if (pmic_type == MT65XX_LED_PMIC_LCD_ISINK)
663 {
664 #if 0
665 if(backlight_init_flag[0] == false)
666 {
667 hwBacklightISINKTuning(1, PMIC_PWM_0, 0x3, 0);
668 hwBacklightISINKTuning(2, PMIC_PWM_0, 0x3, 0);
669 hwBacklightISINKTuning(3, PMIC_PWM_0, 0x3, 0);
670 backlight_init_flag[0] = true;
671 }
672
673 if (level)
674 {
675 level = brightness_mapping(tmp_level);
676 if(level == ERROR_BL_LEVEL)
677 level = tmp_level/17;
678 hwPWMsetting(PMIC_PWM_0, level, div);
679 upmu_top2_bst_drv_ck_pdn(0x0);
680 hwBacklightISINKTurnOn(1);
681 hwBacklightISINKTurnOn(2);
682 hwBacklightISINKTurnOn(3);
683 bl_duty = level;
684 }
685 else
686 {
687 hwBacklightISINKTurnOff(1);
688 hwBacklightISINKTurnOff(2);
689 hwBacklightISINKTurnOff(3);
690 bl_duty = level;
691 }
692 return 0;
693 }
694 else if(pmic_type == MT65XX_LED_PMIC_LCD_BOOST)
695 {
696 /*
697 if(backlight_init_flag[1] == false)
698 {
699 hwBacklightBoostTuning(PMIC_PWM_0, 0xC, 0);
700 backlight_init_flag[1] = true;
701 }
702 */
703 if (level)
704 {
705 level = brightness_mapping(tmp_level);
706 if(level == ERROR_BL_LEVEL)
707 level = tmp_level/42;
708
709 upmu_boost_isink_hw_sel(0x1);
710 upmu_boost_mode(3);
711 upmu_boost_cabc_en(0);
712
713 switch(level)
714 {
715 case 0:
716 upmu_boost_vrsel(0x0);
717 //hwPWMsetting(PMIC_PWM_0, 0, 0x15);
718 break;
719 case 1:
720 upmu_boost_vrsel(0x1);
721 //hwPWMsetting(PMIC_PWM_0, 4, 0x15);
722 break;
723 case 2:
724 upmu_boost_vrsel(0x2);
725 //hwPWMsetting(PMIC_PWM_0, 5, 0x15);
726 break;
727 case 3:
728 upmu_boost_vrsel(0x3);
729 //hwPWMsetting(PMIC_PWM_0, 6, 0x15);
730 break;
731 case 4:
732 upmu_boost_vrsel(0x5);
733 //hwPWMsetting(PMIC_PWM_0, 7, 0x15);
734 break;
735 case 5:
736 upmu_boost_vrsel(0x8);
737 //hwPWMsetting(PMIC_PWM_0, 8, 0x15);
738 break;
739 case 6:
740 upmu_boost_vrsel(0xB);
741 //hwPWMsetting(PMIC_PWM_0, 9, 0x15);
742 break;
743 default:
744 printk("[LED] invalid brightness level %d->%d\n", tmp_level, level);
745 break;
746 }
747
748 upmu_top2_bst_drv_ck_pdn(0x0);
749 upmu_boost_en(0x1);
750 bl_duty = level;
751 }
752 else
753 {
754 upmu_boost_en(0x0);
755 bl_duty = level;
756 //upmu_top2_bst_drv_ck_pdn(0x1);
757 }
758 return 0;
759 #endif
760 }
761 #if 0
762 else if (pmic_type == MT65XX_LED_PMIC_BUTTON)
763 {
764
765 if (level)
766 {
767 upmu_set_kpled_dim_duty(0x9);
768 upmu_set_kpled_en(0x1);
769 }
770 else
771 {
772 upmu_set_kpled_en(0x0);
773 }
774 return 0;
775
776 }
777 #endif
778 else if(pmic_type == MT65XX_LED_PMIC_NLED_ISINK0)
779 {
780 if(first_time == true)
781 {
782 upmu_set_isinks_ch1_en(0x0); //sw workround for sync leds status
783 upmu_set_isink_rsv2_isink1(0x00);
784 upmu_set_isinks_ch2_en(0x0);
785 upmu_set_isink_rsv2_isink2(0x00);
786 first_time = false;
787 }
788
789 //hwBacklightISINKTuning(0x0, 0x3, 0x0, 0); //register mode, ch0_step=4ma ,disable CABC
790 // hwBacklightISINKTurnOn(0x0); //turn on ISINK0 77 uses ISINK4&5
791
792
793 //if(led_init_flag[0] == false)
794 {
795 //hwBacklightISINKTuning(MT65XX_LED_PMIC_NLED_ISINK4, PMIC_PWM_1, 0x0, 0);
796 upmu_set_isinks_ch0_mode(PMIC_PWM_0);
797 upmu_set_isinks_ch0_step(0x0);//4mA
798 //hwPWMsetting(PMIC_PWM_1, 15, 8);
799 upmu_set_isink_dim0_duty(15);
800 upmu_set_isink_dim0_fsel(11);//6320 0.25KHz
801 led_init_flag[0] = true;
802 }
803
804 if (level)
805 {
806 //upmu_top2_bst_drv_ck_pdn(0x0);
807 //upmu_set_rg_spk_div_pdn(0x01);
808 //upmu_set_rg_spk_pwm_div_pdn(0x01);
809
810 upmu_set_rg_bst_drv_1m_ck_pdn(0x0);
811 #ifdef ISINK_CHOP_CLK
812 //upmu_set_isinks0_chop_en(0x1);
813 #endif
814 upmu_set_isink_rsv2_isink0(0x1);
815 upmu_set_isinks_ch0_en(0x01);
816
817 }
818 else
819 {
820 upmu_set_isinks_ch0_en(0x00);
821 upmu_set_isink_rsv2_isink0(0x00);
822 //upmu_set_rg_bst_drv_1m_ck_pdn(0x1);
823 //upmu_top2_bst_drv_ck_pdn(0x1);
824 }
825 return 0;
826 }
827 else if(pmic_type == MT65XX_LED_PMIC_NLED_ISINK1)
828 {
829 if(first_time == true)
830 {
831 upmu_set_isinks_ch0_en(0); //sw workround for sync leds status
832 upmu_set_isink_rsv2_isink0(0x00);
833 upmu_set_isinks_ch2_en(0);
834 upmu_set_isink_rsv2_isink2(0x00);
835 first_time = false;
836 }
837
838 //hwBacklightISINKTuning(0x0, 0x3, 0x0, 0); //register mode, ch0_step=4ma ,disable CABC
839 //hwBacklightISINKTurnOn(0x0); //turn on ISINK0
840
841 //if(led_init_flag[1] == false)
842 {
843 //hwBacklightISINKTuning(MT65XX_LED_PMIC_NLED_ISINK5, PMIC_PWM_2, 0x0, 0);
844 upmu_set_isinks_ch1_mode(PMIC_PWM_0);
845 upmu_set_isinks_ch1_step(0x3);//4mA
846 //hwPWMsetting(PMIC_PWM_2, 15, 8);
847 upmu_set_isink_dim1_duty(15);
848 upmu_set_isink_dim1_fsel(11);//6320 0.25KHz
849 led_init_flag[1] = true;
850 }
851 if (level)
852 {
853 //upmu_top2_bst_drv_ck_pdn(0x0);
854
855 //upmu_set_rg_spk_div_pdn(0x01);
856 //upmu_set_rg_spk_pwm_div_pdn(0x01);
857 upmu_set_rg_bst_drv_1m_ck_pdn(0x0);
858 #ifdef ISINK_CHOP_CLK
859 //upmu_set_isinks1_chop_en(0x1);
860 #endif
861 upmu_set_isink_rsv2_isink1(0x1);
862 upmu_set_isinks_ch1_en(0x01);
863 }
864 else
865 {
866 upmu_set_isinks_ch1_en(0x00);
867 upmu_set_isink_rsv2_isink1(0x00);
868 //upmu_set_rg_bst_drv_1m_ck_pdn(0x1);
869 //upmu_top2_bst_drv_ck_pdn(0x1);
870 }
871 return 0;
872 }
873 else if(pmic_type == MT65XX_LED_PMIC_NLED_ISINK2)
874 {
875
876 if(first_time == true)
877 {
878 upmu_set_isinks_ch0_en(0); //sw workround for sync leds status
879 upmu_set_isink_rsv2_isink0(0x00);
880 upmu_set_isinks_ch1_en(0);
881 upmu_set_isink_rsv2_isink1(0x00);
882 first_time = false;
883 }
884
885 //hwBacklightISINKTuning(0x0, 0x3, 0x0, 0); //register mode, ch0_step=4ma ,disable CABC
886 //hwBacklightISINKTurnOn(0x0); //turn on ISINK0
887
888 //if(led_init_flag[1] == false)
889 {
890 //hwBacklightISINKTuning(MT65XX_LED_PMIC_NLED_ISINK5, PMIC_PWM_2, 0x0, 0);
891 upmu_set_isinks_ch2_mode(PMIC_PWM_0);
892 upmu_set_isinks_ch2_step(0x3);//16mA
893 //hwPWMsetting(PMIC_PWM_2, 15, 8);
894 upmu_set_isink_dim2_duty(15);
895 upmu_set_isink_dim2_fsel(11);//6320 0.25KHz
896 led_init_flag[2] = true;
897 }
898 if (level)
899 {
900 //upmu_top2_bst_drv_ck_pdn(0x0);
901
902 //upmu_set_rg_spk_div_pdn(0x01);
903 //upmu_set_rg_spk_pwm_div_pdn(0x01);
904 upmu_set_rg_bst_drv_1m_ck_pdn(0x0);
905 #ifdef ISINK_CHOP_CLK
906 //upmu_set_isinks2_chop_en(0x1);
907 #endif
908 upmu_set_isink_rsv2_isink2(0x1);
909 upmu_set_isinks_ch2_en(0x01);
910 }
911 else
912 {
913 upmu_set_isinks_ch2_en(0x00);
914 upmu_set_isink_rsv2_isink2(0x00);
915 //upmu_set_rg_bst_drv_1m_ck_pdn(0x1);
916 //upmu_top2_bst_drv_ck_pdn(0x1);
917 }
918 return 0;
919 }
920 #if 0
921 else if(pmic_type == MT65XX_LED_PMIC_NLED_ISINK01)
922 {
923
924 //hwBacklightISINKTuning(0x0, 0x3, 0x0, 0); //register mode, ch0_step=4ma ,disable CABC
925 //hwBacklightISINKTurnOn(0x0); //turn on ISINK0
926
927 //if(led_init_flag[1] == false)
928 {
929
930 upmu_set_isinks_ch0_mode(PMIC_PWM_0);
931 upmu_set_isinks_ch0_step(0x0);//4mA
932 upmu_set_isink_dim0_duty(1);
933 upmu_set_isink_dim0_fsel(1);//6320 1.5KHz
934 //hwBacklightISINKTuning(MT65XX_LED_PMIC_NLED_ISINK5, PMIC_PWM_2, 0x0, 0);
935 upmu_set_isinks_ch1_mode(PMIC_PWM_0);
936 upmu_set_isinks_ch1_step(0x3);//4mA
937 //hwPWMsetting(PMIC_PWM_2, 15, 8);
938 upmu_set_isink_dim1_duty(15);
939 upmu_set_isink_dim1_fsel(11);//6320 0.25KHz
940 led_init_flag[0] = true;
941 led_init_flag[1] = true;
942 }
943 if (level)
944 {
945 //upmu_top2_bst_drv_ck_pdn(0x0);
946
947 //upmu_set_rg_spk_div_pdn(0x01);
948 //upmu_set_rg_spk_pwm_div_pdn(0x01);
949 upmu_set_rg_bst_drv_1m_ck_pdn(0x0);
950 #ifdef ISINK_CHOP_CLK
951 //upmu_set_isinks0_chop_en(0x1);
952 //upmu_set_isinks1_chop_en(0x1);
953 #endif
954 upmu_set_isinks_ch0_en(0x01);
955 upmu_set_isinks_ch1_en(0x01);
956 }
957 else
958 {
959 upmu_set_isinks_ch0_en(0x00);
960 upmu_set_isinks_ch1_en(0x00);
961 //upmu_set_rg_bst_drv_1m_ck_pdn(0x1);
962 //upmu_top2_bst_drv_ck_pdn(0x1);
963 }
964 return 0;
965 }
966 #endif
967 return -1;
968
969 }
970
971 #else
972 int mt_brightness_set_pmic(enum mt65xx_led_pmic pmic_type, u32 level, u32 div)
973 {
974 int tmp_level = level;
975 //static bool backlight_init_flag[4] = {false, false, false, false};
976 static bool backlight_init_flag = false;
977 //static bool led_init_flag[4] = {false, false, false, false};
978 static bool first_time = true;
979 static unsigned char duty_mapping[108] = {
980 0, 0, 0, 0, 0, 6, 1, 2, 1, 10, 1, 12,
981 6, 14, 2, 3, 16, 2, 18, 3, 6, 10, 22, 3,
982 12, 8, 6, 28, 4, 30, 7, 10, 16, 6, 5, 18,
983 12, 7, 6, 10, 8, 22, 7, 9, 16, 12, 8, 10,
984 13, 18, 28, 9, 30, 20, 15, 12, 10, 16, 22, 13,
985 11, 14, 18, 12, 19, 15, 26, 13, 16, 28, 21, 14,
986 22, 30, 18, 15, 19, 16, 25, 20, 17, 21, 27, 18,
987 22, 28, 19, 30, 24, 20, 31, 25, 21, 26, 22, 27,
988 23, 28, 24, 30, 25, 31, 26, 27, 28, 29, 30, 31,
989 };
990 static unsigned char current_mapping[108] = {
991 1, 2, 3, 4, 5, 0, 3, 2, 4, 0, 5, 0,
992 1, 0, 4, 3, 0, 5, 0, 4, 2, 1, 0, 5,
993 1, 2, 3, 0, 5, 0, 3, 2, 1, 4, 5, 1,
994 2, 4, 5, 3, 4, 1, 5, 4, 2, 3, 5, 4,
995 3, 2, 1, 5, 1, 2, 3, 4, 5, 3, 2, 4,
996 5, 4, 3, 5, 3, 4, 2, 5, 4, 2, 3, 5,
997 3, 2, 4, 5, 4, 5, 3, 4, 5, 4, 3, 5,
998 4, 3, 5, 3, 4, 5, 3, 4, 5, 4, 5, 4,
999 5, 4, 5, 4, 5, 4, 5, 5, 5, 5, 5, 5,
1000 };
1001
1002 LEDS_DEBUG("[LED]PMIC#%d:%d\n", pmic_type, level);
1003 mutex_lock(&leds_pmic_mutex);
1004
1005 if (pmic_type == MT65XX_LED_PMIC_LCD_ISINK)
1006 {
1007 if(backlight_init_flag == false)
1008 {
1009 upmu_set_rg_drv_2m_ck_pdn(0x0); // Disable power down
1010
1011 // For backlight: Current: 24mA, PWM frequency: 20K, Duty: 20~100, Soft start: off, Phase shift: on
1012 // ISINK0
1013 upmu_set_rg_isink0_ck_pdn(0x0); // Disable power down
1014 upmu_set_rg_isink0_ck_sel(0x1); // Freq = 1Mhz for Backlight
1015 upmu_set_isink_ch0_mode(ISINK_PWM_MODE);
1016 upmu_set_isink_ch0_step(0x5); // 24mA
1017 upmu_set_isink_sfstr0_en(0x0); // Disable soft start
1018 upmu_set_rg_isink0_double_en(0x1); // Enable double current
1019 upmu_set_isink_phase_dly_tc(0x0); // TC = 0.5us
1020 upmu_set_isink_phase0_dly_en(0x1); // Enable phase delay
1021 upmu_set_isink_chop0_en(0x1); // Enable CHOP clk
1022 // ISINK1
1023 upmu_set_rg_isink1_ck_pdn(0x0); // Disable power down
1024 upmu_set_rg_isink1_ck_sel(0x1); // Freq = 1Mhz for Backlight
1025 upmu_set_isink_ch1_mode(ISINK_PWM_MODE);
1026 upmu_set_isink_ch1_step(0x5); // 24mA
1027 upmu_set_isink_sfstr1_en(0x0); // Disable soft start
1028 upmu_set_rg_isink1_double_en(0x1); // Enable double current
1029 upmu_set_isink_phase1_dly_en(0x1); // Enable phase delay
1030 upmu_set_isink_chop1_en(0x1); // Enable CHOP clk
1031 // ISINK2
1032 upmu_set_rg_isink2_ck_pdn(0x0); // Disable power down
1033 upmu_set_rg_isink2_ck_sel(0x1); // Freq = 1Mhz for Backlight
1034 upmu_set_isink_ch2_mode(ISINK_PWM_MODE);
1035 upmu_set_isink_ch2_step(0x5); // 24mA
1036 upmu_set_isink_sfstr2_en(0x0); // Disable soft start
1037 upmu_set_rg_isink2_double_en(0x1); // Enable double current
1038 upmu_set_isink_phase2_dly_en(0x1); // Enable phase delay
1039 upmu_set_isink_chop2_en(0x1); // Enable CHOP clk
1040 // ISINK3
1041 upmu_set_rg_isink3_ck_pdn(0x0); // Disable power down
1042 upmu_set_rg_isink3_ck_sel(0x1); // Freq = 1Mhz for Backlight
1043 upmu_set_isink_ch3_mode(ISINK_PWM_MODE);
1044 upmu_set_isink_ch3_step(0x5); // 24mA
1045 upmu_set_isink_sfstr3_en(0x0); // Disable soft start
1046 upmu_set_rg_isink3_double_en(0x1); // Enable double current
1047 upmu_set_isink_phase3_dly_en(0x1); // Enable phase delay
1048 upmu_set_isink_chop3_en(0x1); // Enable CHOP clk
1049 backlight_init_flag = true;
1050 }
1051
1052 if (level)
1053 {
1054 level = brightness_mapping(tmp_level);
1055 if(level == ERROR_BL_LEVEL)
1056 level = 255;
1057 if(level == 255)
1058 {
1059 level = 108;
1060 }
1061 else
1062 {
1063 level = ((level * 108) / 255) + 1;
1064 }
1065 LEDS_DEBUG("[LED]Level Mapping = %d \n", level);
1066 LEDS_DEBUG("[LED]ISINK DIM Duty = %d \n", duty_mapping[level-1]);
1067 LEDS_DEBUG("[LED]ISINK Current = %d \n", current_mapping[level-1]);
1068 upmu_set_isink_dim0_duty(duty_mapping[level-1]);
1069 upmu_set_isink_dim1_duty(duty_mapping[level-1]);
1070 upmu_set_isink_dim2_duty(duty_mapping[level-1]);
1071 upmu_set_isink_dim3_duty(duty_mapping[level-1]);
1072 upmu_set_isink_ch0_step(current_mapping[level-1]);
1073 upmu_set_isink_ch1_step(current_mapping[level-1]);
1074 upmu_set_isink_ch2_step(current_mapping[level-1]);
1075 upmu_set_isink_ch3_step(current_mapping[level-1]);
1076 upmu_set_isink_dim0_fsel(0x2); // 20Khz
1077 upmu_set_isink_dim1_fsel(0x2); // 20Khz
1078 upmu_set_isink_dim2_fsel(0x2); // 20Khz
1079 upmu_set_isink_dim3_fsel(0x2); // 20Khz
1080 upmu_set_isink_ch0_en(0x1); // Turn on ISINK Channel 0
1081 upmu_set_isink_ch1_en(0x1); // Turn on ISINK Channel 1
1082 upmu_set_isink_ch2_en(0x1); // Turn on ISINK Channel 2
1083 upmu_set_isink_ch3_en(0x1); // Turn on ISINK Channel 3
1084 bl_duty_hal = level;
1085 }
1086 else
1087 {
1088 upmu_set_isink_ch0_en(0x0); // Turn off ISINK Channel 0
1089 upmu_set_isink_ch1_en(0x0); // Turn off ISINK Channel 1
1090 upmu_set_isink_ch2_en(0x0); // Turn off ISINK Channel 2
1091 upmu_set_isink_ch3_en(0x0); // Turn off ISINK Channel 3
1092 bl_duty_hal = level;
1093 }
1094 mutex_unlock(&leds_pmic_mutex);
1095 return 0;
1096 }
1097 else if(pmic_type == MT65XX_LED_PMIC_NLED_ISINK0)
1098 {
1099 if((button_flag_isink0==0) && (first_time == true)) {//button flag ==0, means this ISINK is not for button backlight
1100 if(button_flag_isink1==0)
1101 upmu_set_isink_ch1_en(0);
1102 if(button_flag_isink2==0)
1103 upmu_set_isink_ch2_en(0);
1104 if(button_flag_isink3==0)
1105 upmu_set_isink_ch3_en(0);
1106 first_time = false;
1107 }
1108
1109 upmu_set_rg_drv_32k_ck_pdn(0x0); // Disable power down
1110 upmu_set_rg_isink0_ck_pdn(0);
1111 upmu_set_rg_isink0_ck_sel(0);
1112 upmu_set_isink_ch0_mode(PMIC_PWM_0);
1113 upmu_set_isink_ch0_step(0x3);//16mA
1114 //hwPWMsetting(PMIC_PWM_1, 15, 8);
1115 upmu_set_isink_dim0_duty(15);
1116 upmu_set_isink_dim0_fsel(0);//6323 1KHz
1117
1118
1119 if (level)
1120 {
1121
1122 upmu_set_isink_ch0_en(0x01);
1123
1124 }
1125 else
1126 {
1127 upmu_set_isink_ch0_en(0x0);
1128 }
1129 mutex_unlock(&leds_pmic_mutex);
1130 return 0;
1131 }
1132 else if(pmic_type == MT65XX_LED_PMIC_NLED_ISINK1)
1133 {
1134 if((button_flag_isink1==0) && (first_time == true)) {//button flag ==0, means this ISINK is not for button backlight
1135 if(button_flag_isink0==0)
1136 upmu_set_isink_ch0_en(0);
1137 if(button_flag_isink2==0)
1138 upmu_set_isink_ch2_en(0);
1139 if(button_flag_isink3==0)
1140 upmu_set_isink_ch3_en(0);
1141 first_time = false;
1142 }
1143
1144 upmu_set_rg_drv_32k_ck_pdn(0x0); // Disable power down
1145 upmu_set_rg_isink1_ck_pdn(0);
1146 upmu_set_rg_isink1_ck_sel(0);
1147 upmu_set_isink_ch1_mode(PMIC_PWM_0);
1148 upmu_set_isink_ch1_step(0x3);//16mA
1149 //hwPWMsetting(PMIC_PWM_2, 15, 8);
1150 upmu_set_isink_dim1_duty(15);
1151 upmu_set_isink_dim1_fsel(0);//6323 1KHz
1152
1153 if (level)
1154 {
1155 #ifdef NO_NEED_USB_LED
1156 LEDS_DEBUG("[LED]NO_NEED_USB_LED +++++++++++++++\n");
1157 if( FACTORY_BOOT == get_boot_mode() || META_BOOT == get_boot_mode()) {
1158 upmu_set_isink_ch1_en(0x1);
1159 }else {
1160 upmu_set_isink_ch1_en(0x0);
1161 }
1162 #else
1163 upmu_set_isink_ch1_en(0x01);
1164 #endif
1165 }
1166 else
1167 {
1168 upmu_set_isink_ch1_en(0x0);
1169 }
1170 mutex_unlock(&leds_pmic_mutex);
1171 return 0;
1172 }
1173 else if(pmic_type == MT65XX_LED_PMIC_NLED_ISINK2)
1174 {
1175 if((button_flag_isink2==0) && (first_time == true)) {//button flag ==0, means this ISINK is not for button backlight
1176 if(button_flag_isink0==0)
1177 upmu_set_isink_ch0_en(0);
1178 if(button_flag_isink1==0)
1179 upmu_set_isink_ch1_en(0);
1180 if(button_flag_isink3==0)
1181 upmu_set_isink_ch3_en(0);
1182 first_time = false;
1183 }
1184 upmu_set_rg_drv_32k_ck_pdn(0x0); // Disable power down
1185 upmu_set_rg_isink2_ck_pdn(0);
1186 upmu_set_rg_isink2_ck_sel(0);
1187 upmu_set_isink_ch2_mode(PMIC_PWM_0);
1188 upmu_set_isink_ch2_step(0x3);//16mA
1189 //hwPWMsetting(PMIC_PWM_2, 15, 8);
1190 upmu_set_isink_dim2_duty(15);
1191 upmu_set_isink_dim2_fsel(0);//6323 1KHz
1192
1193
1194 if (level)
1195 {
1196 upmu_set_isink_ch2_en(0x01);
1197 }
1198 else
1199 {
1200 upmu_set_isink_ch2_en(0x0);
1201 }
1202 mutex_unlock(&leds_pmic_mutex);
1203 return 0;
1204 }
1205 else if(pmic_type == MT65XX_LED_PMIC_NLED_ISINK3)
1206 {
1207 if((button_flag_isink3==0) && (first_time == true)) {//button flag ==0, means this ISINK is not for button backlight
1208 if(button_flag_isink0==0)
1209 upmu_set_isink_ch0_en(0);
1210 if(button_flag_isink1==0)
1211 upmu_set_isink_ch1_en(0);
1212 if(button_flag_isink2==0)
1213 upmu_set_isink_ch2_en(0);
1214 first_time = false;
1215 }
1216
1217 upmu_set_rg_drv_32k_ck_pdn(0x0); // Disable power down
1218 upmu_set_rg_isink3_ck_pdn(0);
1219 upmu_set_rg_isink3_ck_sel(0);
1220 upmu_set_isink_ch3_mode(PMIC_PWM_0);
1221 upmu_set_isink_ch3_step(0x3);//16mA
1222 //hwPWMsetting(PMIC_PWM_1, 15, 8);
1223 upmu_set_isink_dim3_duty(15);
1224 upmu_set_isink_dim3_fsel(0);//6323 1KHz
1225
1226
1227 if (level)
1228 {
1229 upmu_set_isink_ch3_en(0x01);
1230
1231 }
1232 else
1233 {
1234 upmu_set_isink_ch3_en(0x0);
1235 }
1236 mutex_unlock(&leds_pmic_mutex);
1237 return 0;
1238 }
1239 #if 0
1240 else if(pmic_type == MT65XX_LED_PMIC_NLED_ISINK01)
1241 {
1242
1243 //hwBacklightISINKTuning(0x0, 0x3, 0x0, 0); //register mode, ch0_step=4ma ,disable CABC
1244 //hwBacklightISINKTurnOn(0x0); //turn on ISINK0
1245
1246 //if(led_init_flag[1] == false)
1247 {
1248
1249 upmu_set_isinks_ch0_mode(PMIC_PWM_0);
1250 upmu_set_isinks_ch0_step(0x0);//4mA
1251 upmu_set_isink_dim0_duty(1);
1252 upmu_set_isink_dim0_fsel(1);//6320 1.5KHz
1253 //hwBacklightISINKTuning(MT65XX_LED_PMIC_NLED_ISINK5, PMIC_PWM_2, 0x0, 0);
1254 upmu_set_isinks_ch1_mode(PMIC_PWM_0);
1255 upmu_set_isinks_ch1_step(0x3);//4mA
1256 //hwPWMsetting(PMIC_PWM_2, 15, 8);
1257 upmu_set_isink_dim1_duty(15);
1258 upmu_set_isink_dim1_fsel(11);//6320 0.25KHz
1259 led_init_flag[0] = true;
1260 led_init_flag[1] = true;
1261 }
1262 if (level)
1263 {
1264 //upmu_top2_bst_drv_ck_pdn(0x0);
1265
1266 //upmu_set_rg_spk_div_pdn(0x01);
1267 //upmu_set_rg_spk_pwm_div_pdn(0x01);
1268 upmu_set_rg_bst_drv_1m_ck_pdn(0x0);
1269 #ifdef ISINK_CHOP_CLK
1270 //upmu_set_isinks0_chop_en(0x1);
1271 //upmu_set_isinks1_chop_en(0x1);
1272 #endif
1273 upmu_set_isinks_ch0_en(0x01);
1274 upmu_set_isinks_ch1_en(0x01);
1275 }
1276 else
1277 {
1278 upmu_set_isinks_ch0_en(0x00);
1279 upmu_set_isinks_ch1_en(0x00);
1280 //upmu_set_rg_bst_drv_1m_ck_pdn(0x1);
1281 //upmu_top2_bst_drv_ck_pdn(0x1);
1282 }
1283 return 0;
1284 }
1285 #endif
1286
1287 mutex_unlock(&leds_pmic_mutex);
1288 return -1;
1289
1290 }
1291 #endif
1292
1293 int mt_brightness_set_pmic_duty_store(u32 level, u32 div)
1294 {
1295 return -1;
1296 }
1297 int mt_mt65xx_led_set_cust(struct cust_mt65xx_led *cust, int level)
1298 {
1299 struct nled_setting led_tmp_setting = {0,0,0};
1300 int tmp_level = level;
1301 static bool button_flag = false;
1302 unsigned int BacklightLevelSupport = Cust_GetBacklightLevelSupport_byPWM();
1303 //Mark out since the level is already cliped before sending in
1304 /*
1305 if (level > LED_FULL)
1306 level = LED_FULL;
1307 else if (level < 0)
1308 level = 0;
1309 */
1310 LEDS_DEBUG("mt65xx_leds_set_cust: set brightness, name:%s, mode:%d, level:%d\n",
1311 cust->name, cust->mode, level);
1312 switch (cust->mode) {
1313
1314 case MT65XX_LED_MODE_PWM:
1315 if(strcmp(cust->name,"lcd-backlight") == 0)
1316 {
1317 bl_brightness_hal = level;
1318 if(level == 0)
1319 {
1320 //mt_set_pwm_disable(cust->data);
1321 //mt_pwm_power_off (cust->data);
1322 mt_pwm_disable(cust->data, cust->config_data.pmic_pad);
1323
1324 }else
1325 {
1326 if (BacklightLevelSupport == BACKLIGHT_LEVEL_PWM_256_SUPPORT)
1327 level = brightness_mapping(tmp_level);
1328 else
1329 level = brightness_mapto64(tmp_level);
1330 mt_backlight_set_pwm(cust->data, level, bl_div_hal,&cust->config_data);
1331 }
1332 bl_duty_hal = level;
1333
1334 }else
1335 {
1336 if(level == 0)
1337 {
1338 led_tmp_setting.nled_mode = NLED_OFF;
1339 mt_led_set_pwm(cust->data,&led_tmp_setting);
1340 mt_pwm_disable(cust->data, cust->config_data.pmic_pad);
1341 }else
1342 {
1343 led_tmp_setting.nled_mode = NLED_ON;
1344 mt_led_set_pwm(cust->data,&led_tmp_setting);
1345 }
1346
1347 }
1348 return 1;
1349
1350 case MT65XX_LED_MODE_GPIO:
1351 LEDS_DEBUG("brightness_set_cust:go GPIO mode!!!!!\n");
1352 return ((cust_set_brightness)(cust->data))(level);
1353
1354 case MT65XX_LED_MODE_PMIC:
1355 //for button baclight used SINK channel, when set button ISINK, don't do disable other ISINK channel
1356 if((strcmp(cust->name,"button-backlight") == 0)) {
1357 if(button_flag==false) {
1358 switch (cust->data) {
1359 case MT65XX_LED_PMIC_NLED_ISINK0:
1360 button_flag_isink0 = 1;
1361 break;
1362 case MT65XX_LED_PMIC_NLED_ISINK1:
1363 button_flag_isink1 = 1;
1364 break;
1365 case MT65XX_LED_PMIC_NLED_ISINK2:
1366 button_flag_isink2 = 1;
1367 break;
1368 case MT65XX_LED_PMIC_NLED_ISINK3:
1369 button_flag_isink3 = 1;
1370 break;
1371 default:
1372 break;
1373 }
1374 button_flag=true;
1375 }
1376 }
1377 return mt_brightness_set_pmic(cust->data, level, bl_div_hal);
1378
1379 case MT65XX_LED_MODE_CUST_LCM:
1380 if(strcmp(cust->name,"lcd-backlight") == 0)
1381 {
1382 bl_brightness_hal = level;
1383 }
1384 LEDS_DEBUG("brightness_set_cust:backlight control by LCM\n");
1385 return ((cust_brightness_set)(cust->data))(level, bl_div_hal);
1386
1387
1388 case MT65XX_LED_MODE_CUST_BLS_PWM:
1389 if(strcmp(cust->name,"lcd-backlight") == 0)
1390 {
1391 bl_brightness_hal = level;
1392 }
1393 //LEDS_DEBUG("brightness_set_cust:backlight control by BLS_PWM!!\n");
1394 //#if !defined (MTK_AAL_SUPPORT)
1395 return ((cust_set_brightness)(cust->data))(level);
1396 //LEDS_DEBUG("brightness_set_cust:backlight control by BLS_PWM done!!\n");
1397 //#endif
1398
1399 case MT65XX_LED_MODE_NONE:
1400 default:
1401 break;
1402 }
1403 return -1;
1404 }
1405
1406 void mt_mt65xx_led_work(struct work_struct *work)
1407 {
1408 struct mt65xx_led_data *led_data =
1409 container_of(work, struct mt65xx_led_data, work);
1410
1411 LEDS_DEBUG("[LED]%s:%d\n", led_data->cust.name, led_data->level);
1412 mutex_lock(&leds_mutex);
1413 mt_mt65xx_led_set_cust(&led_data->cust, led_data->level);
1414 mutex_unlock(&leds_mutex);;
1415 }
1416
1417 void mt_mt65xx_led_set(struct led_classdev *led_cdev, enum led_brightness level)
1418 {
1419 struct mt65xx_led_data *led_data =
1420 container_of(led_cdev, struct mt65xx_led_data, cdev);
1421 //unsigned long flags;
1422 //spin_lock_irqsave(&leds_lock, flags);
1423
1424 #ifdef CONFIG_MTK_AAL_SUPPORT
1425 if(led_data->level != level)
1426 {
1427 led_data->level = level;
1428 if(strcmp(led_data->cust.name,"lcd-backlight") != 0)
1429 {
1430 LEDS_DEBUG("[LED]Set NLED directly %d at time %lu\n",led_data->level,jiffies);
1431 schedule_work(&led_data->work);
1432 }
1433 else
1434 {
1435 LEDS_DEBUG("[LED]Set Backlight directly %d at time %lu\n",led_data->level,jiffies);
1436 //mt_mt65xx_led_set_cust(&led_data->cust, led_data->level);
1437 disp_aal_notify_backlight_changed( (((1 << MT_LED_INTERNAL_LEVEL_BIT_CNT) - 1)*level + 127)/255 );
1438 }
1439 }
1440 #else
1441 // do something only when level is changed
1442 if(led_data->level != level)
1443 {
1444 led_data->level = level;
1445 if(strcmp(led_data->cust.name,"lcd-backlight") != 0)
1446 {
1447 LEDS_DEBUG("[LED]Set NLED directly %d at time %lu\n",led_data->level,jiffies);
1448 schedule_work(&led_data->work);
1449 }
1450 else
1451 {
1452 LEDS_DEBUG("[LED]Set Backlight directly %d at time %lu\n",led_data->level,jiffies);
1453 if(MT65XX_LED_MODE_CUST_BLS_PWM == led_data->cust.mode)
1454 {
1455 mt_mt65xx_led_set_cust(&led_data->cust, ((((1 << MT_LED_INTERNAL_LEVEL_BIT_CNT) - 1)*level + 127)/255));
1456 }
1457 else
1458 {
1459 mt_mt65xx_led_set_cust(&led_data->cust, led_data->level);
1460 }
1461 }
1462 }
1463 //spin_unlock_irqrestore(&leds_lock, flags);
1464 #endif
1465 aee_kernel_wdt_kick_Powkey_api("mt_mt65xx_led_set",WDT_SETBY_Backlight);
1466
1467 }
1468
1469 int mt_mt65xx_blink_set(struct led_classdev *led_cdev,
1470 unsigned long *delay_on,
1471 unsigned long *delay_off)
1472 {
1473 struct mt65xx_led_data *led_data =
1474 container_of(led_cdev, struct mt65xx_led_data, cdev);
1475 static int got_wake_lock = 0;
1476 struct nled_setting nled_tmp_setting = {0,0,0};
1477
1478 // only allow software blink when delay_on or delay_off changed
1479 if (*delay_on != led_data->delay_on || *delay_off != led_data->delay_off) {
1480 led_data->delay_on = *delay_on;
1481 led_data->delay_off = *delay_off;
1482 if (led_data->delay_on && led_data->delay_off) { // enable blink
1483 led_data->level = 255; // when enable blink then to set the level (255)
1484 //if(led_data->cust.mode == MT65XX_LED_MODE_PWM &&
1485 //(led_data->cust.data != PWM3 && led_data->cust.data != PWM4 && led_data->cust.data != PWM5))
1486
1487 //AP PWM all support OLD mode in MT6589
1488 if(led_data->cust.mode == MT65XX_LED_MODE_PWM)
1489 {
1490 nled_tmp_setting.nled_mode = NLED_BLINK;
1491 nled_tmp_setting.blink_off_time = led_data->delay_off;
1492 nled_tmp_setting.blink_on_time = led_data->delay_on;
1493 mt_led_set_pwm(led_data->cust.data,&nled_tmp_setting);
1494 return 0;
1495 }
1496 else if((led_data->cust.mode == MT65XX_LED_MODE_PMIC) && (led_data->cust.data == MT65XX_LED_PMIC_NLED_ISINK0
1497 || led_data->cust.data == MT65XX_LED_PMIC_NLED_ISINK1 || led_data->cust.data == MT65XX_LED_PMIC_NLED_ISINK2
1498 || led_data->cust.data == MT65XX_LED_PMIC_NLED_ISINK3))
1499 {
1500 //if(get_chip_eco_ver() == CHIP_E2) {
1501 if(1){
1502 nled_tmp_setting.nled_mode = NLED_BLINK;
1503 nled_tmp_setting.blink_off_time = led_data->delay_off;
1504 nled_tmp_setting.blink_on_time = led_data->delay_on;
1505 mt_led_blink_pmic(led_data->cust.data, &nled_tmp_setting);
1506 return 0;
1507 } else {
1508 wake_lock(&leds_suspend_lock);
1509 }
1510 }
1511 else if (!got_wake_lock) {
1512 wake_lock(&leds_suspend_lock);
1513 got_wake_lock = 1;
1514 }
1515 }
1516 else if (!led_data->delay_on && !led_data->delay_off) { // disable blink
1517 //if(led_data->cust.mode == MT65XX_LED_MODE_PWM &&
1518 //(led_data->cust.data != PWM3 && led_data->cust.data != PWM4 && led_data->cust.data != PWM5))
1519
1520 //AP PWM all support OLD mode in MT6589
1521
1522 if(led_data->cust.mode == MT65XX_LED_MODE_PWM)
1523 {
1524 nled_tmp_setting.nled_mode = NLED_OFF;
1525 mt_led_set_pwm(led_data->cust.data,&nled_tmp_setting);
1526 return 0;
1527 }
1528 else if((led_data->cust.mode == MT65XX_LED_MODE_PMIC) && (led_data->cust.data == MT65XX_LED_PMIC_NLED_ISINK0
1529 || led_data->cust.data == MT65XX_LED_PMIC_NLED_ISINK1 || led_data->cust.data == MT65XX_LED_PMIC_NLED_ISINK2
1530 || led_data->cust.data == MT65XX_LED_PMIC_NLED_ISINK3))
1531 {
1532 //if(get_chip_eco_ver() == CHIP_E2) {
1533 if(1){
1534 mt_brightness_set_pmic(led_data->cust.data, 0, 0);
1535 return 0;
1536 } else {
1537 wake_unlock(&leds_suspend_lock);
1538 }
1539 }
1540 else if (got_wake_lock) {
1541 wake_unlock(&leds_suspend_lock);
1542 got_wake_lock = 0;
1543 }
1544 }
1545 return -1;
1546 }
1547
1548 // delay_on and delay_off are not changed
1549 return 0;
1550 }
1551
1552