ddp_pwm.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. *
  31. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #define LOG_TAG "PWM"
  36. #include <string.h>
  37. #include <platform/sync_write.h>
  38. #include <cust_leds.h>
  39. #include <debug.h>
  40. #include <platform/ddp_reg.h>
  41. #include <platform/ddp_pwm.h>
  42. #include <platform/ddp_info.h>
  43. #include <platform/ddp_log.h>
  44. extern int ddp_enable_module_clock(DISP_MODULE_ENUM module);
  45. #ifndef CLK_CFG_7
  46. #define CLK_CFG_7 0x102100B0
  47. #endif
  48. #define PWM_REG_SET(reg, value) do { \
  49. mt65xx_reg_sync_writel(value, (volatile unsigned int*)(reg)); \
  50. PWM_MSG("set reg[0x%08x] = 0x%08x", (unsigned int)reg, (unsigned int)value); \
  51. } while (0)
  52. #define PWM_REG_GET(reg) *((volatile unsigned int*)reg)
  53. #define PWM_DEFAULT_DIV_VALUE 0x0
  54. #define PWM_MSG(fmt, arg...) DDPMSG("[PWM] " fmt "\n", ##arg)
  55. #define pwm_get_reg_base(id) ((id) == DISP_PWM0 ? DISPSYS_PWM0_BASE : DISPSYS_PWM1_BASE)
  56. #define index_of_pwm(id) ((id == DISP_PWM0) ? 0 : 1)
  57. static int g_pwm_inited;
  58. static disp_pwm_id_t g_pwm_main_id = DISP_PWM0;
  59. void disp_pwm_init(disp_pwm_id_t id)
  60. {
  61. struct cust_mt65xx_led *cust_led_list;
  62. struct cust_mt65xx_led *cust;
  63. struct PWM_config *config_data;
  64. unsigned int pwm_div;
  65. unsigned int reg_base = pwm_get_reg_base(id);
  66. if (g_pwm_inited & id)
  67. return;
  68. if (id & DISP_PWM0)
  69. ddp_enable_module_clock(DISP_MODULE_PWM0);
  70. if (id & DISP_PWM1)
  71. ddp_enable_module_clock(DISP_MODULE_PWM1);
  72. pwm_div = PWM_DEFAULT_DIV_VALUE;
  73. cust_led_list = get_cust_led_list();
  74. if (cust_led_list) {
  75. /* WARNING: may overflow if MT65XX_LED_TYPE_LCD not configured properly */
  76. cust = &cust_led_list[MT65XX_LED_TYPE_LCD];
  77. if ((strcmp(cust->name, "lcd-backlight") == 0)
  78. && (cust->mode == MT65XX_LED_MODE_CUST_BLS_PWM)) {
  79. config_data = &cust->config_data;
  80. if (config_data->clock_source >= 0 && config_data->clock_source <= 3) {
  81. unsigned int reg_val = DISP_REG_GET(CLK_CFG_7);
  82. PWM_REG_SET(CLK_CFG_7,
  83. (reg_val & ~0x3) | config_data->clock_source);
  84. PWM_MSG("disp_pwm_init : CLK_CFG_7 0x%x => 0x%x", reg_val,
  85. PWM_REG_GET(CLK_CFG_7));
  86. }
  87. /* Some backlight chip/PMIC(e.g. MT6332) only accept slower clock */
  88. pwm_div =
  89. (config_data->div == 0) ? PWM_DEFAULT_DIV_VALUE : config_data->div;
  90. pwm_div &= 0x3FF;
  91. PWM_MSG("disp_pwm_init : PWM config data (%d,%d)",
  92. config_data->clock_source, config_data->div);
  93. }
  94. }
  95. PWM_REG_SET(reg_base + DISP_PWM_CON_0_OFF, pwm_div << 16);
  96. PWM_REG_SET(reg_base + DISP_PWM_CON_1_OFF, 1023); /* 1024 levels */
  97. g_pwm_inited |= id;
  98. }
  99. disp_pwm_id_t disp_pwm_get_main(void)
  100. {
  101. return g_pwm_main_id;
  102. }
  103. int disp_pwm_is_enabled(disp_pwm_id_t id)
  104. {
  105. unsigned int reg_base = pwm_get_reg_base(id);
  106. return (PWM_REG_GET(reg_base + DISP_PWM_EN_OFF) & 0x1);
  107. }
  108. static void disp_pwm_set_enabled(disp_pwm_id_t id, int enabled)
  109. {
  110. unsigned int reg_base = pwm_get_reg_base(id);
  111. if (enabled) {
  112. if (!disp_pwm_is_enabled(id)) {
  113. PWM_REG_SET(reg_base + DISP_PWM_EN_OFF, 0x1);
  114. PWM_MSG("disp_pwm_set_enabled: PWN_EN = 0x1");
  115. }
  116. } else {
  117. PWM_REG_SET(reg_base + DISP_PWM_EN_OFF, 0x0);
  118. }
  119. }
  120. /* For backward compatible */
  121. int disp_bls_set_backlight(int level_256)
  122. {
  123. int level_1024 = 0;
  124. if (level_256 <= 0)
  125. level_1024 = 0;
  126. else if (level_256 >= 255)
  127. level_1024 = 1023;
  128. else {
  129. level_1024 = (level_256 << 2) + 2;
  130. }
  131. return disp_pwm_set_backlight(disp_pwm_get_main(), level_1024);
  132. }
  133. static int disp_pwm_level_remap(disp_pwm_id_t id, int level_1024)
  134. {
  135. return level_1024;
  136. }
  137. int disp_pwm_set_backlight(disp_pwm_id_t id, int level_1024)
  138. {
  139. unsigned int reg_base;
  140. unsigned int offset;
  141. if ((DISP_PWM_ALL & id) == 0) {
  142. PWM_MSG("[ERROR] disp_pwm_set_backlight: invalid PWM ID = 0x%x", id);
  143. return -1;
  144. }
  145. disp_pwm_init(id);
  146. PWM_MSG("disp_pwm_set_backlight(id = 0x%x, level_1024 = %d)", id, level_1024);
  147. reg_base = pwm_get_reg_base(id);
  148. level_1024 = disp_pwm_level_remap(id, level_1024);
  149. PWM_REG_SET(reg_base + DISP_PWM_COMMIT_OFF, 0);
  150. PWM_REG_SET(reg_base + DISP_PWM_CON_1_OFF, (level_1024 << 16) | 0x3ff);
  151. if (level_1024 > 0) {
  152. disp_pwm_set_enabled(id, 1);
  153. } else {
  154. disp_pwm_set_enabled(id, 0); /* To save power */
  155. }
  156. PWM_REG_SET(reg_base + DISP_PWM_COMMIT_OFF, 1);
  157. PWM_REG_SET(reg_base + DISP_PWM_COMMIT_OFF, 0);
  158. for (offset = 0x0; offset <= 0x28; offset += 4) {
  159. PWM_MSG("reg[0x%08x] = 0x%08x", (reg_base + offset),
  160. PWM_REG_GET(reg_base + offset));
  161. }
  162. return 0;
  163. }