ddp_pwm.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <string.h>
  2. #include <platform/sync_write.h>
  3. #include <cust_leds.h>
  4. #include <debug.h>
  5. #include <platform/ddp_reg.h>
  6. #include <platform/ddp_pwm.h>
  7. #include <platform/ddp_info.h>
  8. #include <platform/ddp_log.h>
  9. extern int ddp_enable_module_clock(DISP_MODULE_ENUM module);
  10. #ifndef CLK_CFG_1
  11. #define CLK_CFG_1 0x10000050
  12. #endif
  13. #ifndef CLK_MUX_SEL0
  14. #define CLK_MUX_SEL0 0x10000000
  15. #endif
  16. #ifndef CLK_GATING_CTRL
  17. #define CLK_GATING_CTRL 0x10000020
  18. #endif
  19. #define PWM_REG_SET(reg, value) do { \
  20. mt65xx_reg_sync_writel(value, (volatile unsigned int*)(reg)); \
  21. PWM_MSG("set reg[0x%08x] = 0x%08x", (unsigned int)reg, (unsigned int)value); \
  22. } while (0)
  23. #define PWM_REG_GET(reg) *((volatile unsigned int*)reg)
  24. #define PWM_DEFAULT_DIV_VALUE 0x0
  25. #define PWM_MSG(fmt, arg...) DDPMSG("[PWM] " fmt "\n", ##arg)
  26. #define pwm_get_reg_base(id) ((id) == DISP_PWM0 ? DISPSYS_PWM0_BASE : DISPSYS_PWM1_BASE)
  27. #define index_of_pwm(id) ((id == DISP_PWM0) ? 0 : 1)
  28. static int g_pwm_inited = 0;
  29. static disp_pwm_id_t g_pwm_main_id = DISP_PWM0;
  30. void disp_pwm_init(disp_pwm_id_t id)
  31. {
  32. struct cust_mt65xx_led *cust_led_list;
  33. struct cust_mt65xx_led *cust;
  34. struct PWM_config *config_data;
  35. unsigned int pwm_div;
  36. unsigned int reg_base = pwm_get_reg_base(id);
  37. if (g_pwm_inited & id)
  38. return;
  39. if (id & DISP_PWM0)
  40. ddp_enable_module_clock(DISP_MODULE_PWM0);
  41. if (id & DISP_PWM1)
  42. ddp_enable_module_clock(DISP_MODULE_PWM1);
  43. pwm_div = PWM_DEFAULT_DIV_VALUE;
  44. cust_led_list = get_cust_led_list();
  45. if (cust_led_list) {
  46. /* WARNING: may overflow if MT65XX_LED_TYPE_LCD not configured properly */
  47. cust = &cust_led_list[MT65XX_LED_TYPE_LCD];
  48. if ((strcmp(cust->name,"lcd-backlight") == 0) && (cust->mode == MT65XX_LED_MODE_CUST_BLS_PWM)) {
  49. config_data = &cust->config_data;
  50. if (config_data->clock_source >= 0 && config_data->clock_source <= 1) {
  51. unsigned int reg_cg = PWM_REG_GET(CLK_GATING_CTRL);
  52. unsigned int reg_val = PWM_REG_GET(CLK_MUX_SEL0);
  53. PWM_REG_SET(CLK_GATING_CTRL, (reg_cg | 0x1));
  54. if (config_data->clock_source == 0)
  55. PWM_REG_SET(CLK_MUX_SEL0, (reg_val & 0xfffbffff));
  56. else if (config_data->clock_source == 1)
  57. PWM_REG_SET(CLK_MUX_SEL0, (reg_val | 0x00040000));
  58. PWM_REG_SET(CLK_GATING_CTRL, (reg_cg & 0xfffffffe));
  59. PWM_MSG("disp_pwm_init : CLK_MUX_SEL0 0x%x => 0x%x", reg_val, PWM_REG_GET(CLK_MUX_SEL0));
  60. }
  61. /* Some backlight chip/PMIC(e.g. MT6332) only accept slower clock */
  62. pwm_div = (config_data->div == 0) ? PWM_DEFAULT_DIV_VALUE : config_data->div;
  63. pwm_div &= 0x3FF;
  64. PWM_MSG("disp_pwm_init : PWM config data (%d,%d)", config_data->clock_source, config_data->div);
  65. }
  66. }
  67. PWM_REG_SET(reg_base + DISP_PWM_CON_0_OFF, pwm_div << 16);
  68. PWM_REG_SET(reg_base + DISP_PWM_CON_1_OFF, 1023); /* 1024 levels */
  69. g_pwm_inited |= id;
  70. }
  71. disp_pwm_id_t disp_pwm_get_main(void)
  72. {
  73. return g_pwm_main_id;
  74. }
  75. int disp_pwm_is_enabled(disp_pwm_id_t id)
  76. {
  77. unsigned int reg_base = pwm_get_reg_base(id);
  78. return (PWM_REG_GET(reg_base + DISP_PWM_EN_OFF) & 0x1);
  79. }
  80. static void disp_pwm_set_enabled(disp_pwm_id_t id, int enabled)
  81. {
  82. unsigned int reg_base = pwm_get_reg_base(id);
  83. if (enabled) {
  84. if (!disp_pwm_is_enabled(id)) {
  85. PWM_REG_SET(reg_base + DISP_PWM_EN_OFF, 0x1);
  86. PWM_MSG("disp_pwm_set_enabled: PWN_EN = 0x1");
  87. }
  88. } else {
  89. PWM_REG_SET(reg_base + DISP_PWM_EN_OFF, 0x0);
  90. }
  91. }
  92. /* For backward compatible */
  93. int disp_bls_set_backlight(int level_256)
  94. {
  95. int level_1024 = 0;
  96. if (level_256 <= 0)
  97. level_1024 = 0;
  98. else if (level_256 >= 255)
  99. level_1024 = 1023;
  100. else {
  101. level_1024 = (level_256 << 2) + 2;
  102. }
  103. return disp_pwm_set_backlight(disp_pwm_get_main(), level_1024);
  104. }
  105. static int disp_pwm_level_remap(disp_pwm_id_t id, int level_1024)
  106. {
  107. return level_1024;
  108. }
  109. int disp_pwm_set_backlight(disp_pwm_id_t id, int level_1024)
  110. {
  111. unsigned int reg_base;
  112. unsigned int offset;
  113. if ((DISP_PWM_ALL & id) == 0) {
  114. PWM_MSG("[ERROR] disp_pwm_set_backlight: invalid PWM ID = 0x%x", id);
  115. return -1;
  116. }
  117. disp_pwm_init(id);
  118. PWM_MSG("disp_pwm_set_backlight(id = 0x%x, level_1024 = %d)", id, level_1024);
  119. reg_base = pwm_get_reg_base(id);
  120. level_1024 = disp_pwm_level_remap(id, level_1024);
  121. PWM_REG_SET(reg_base + DISP_PWM_COMMIT_OFF, 0);
  122. PWM_REG_SET(reg_base + DISP_PWM_CON_1_OFF, (level_1024 << 16) | 0x3ff);
  123. if (level_1024 > 0) {
  124. disp_pwm_set_enabled(id, 1);
  125. } else {
  126. disp_pwm_set_enabled(id, 0); /* To save power */
  127. }
  128. PWM_REG_SET(reg_base + DISP_PWM_COMMIT_OFF, 1);
  129. PWM_REG_SET(reg_base + DISP_PWM_COMMIT_OFF, 0);
  130. for (offset = 0x0; offset <= 0x28; offset += 4) {
  131. PWM_MSG("reg[0x%08x] = 0x%08x", (reg_base + offset), PWM_REG_GET(reg_base + offset));
  132. }
  133. return 0;
  134. }