ddp_pwm.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #define LOG_TAG "PWM"
  32. #include <string.h>
  33. #include <platform/sync_write.h>
  34. #include <cust_leds.h>
  35. #include <debug.h>
  36. #include <platform/ddp_reg.h>
  37. #include <platform/ddp_pwm.h>
  38. #include "platform/ddp_info.h"
  39. #include <platform/disp_drv_log.h>
  40. extern int ddp_enable_module_clock(DISP_MODULE_ENUM module);
  41. //#define BYPASS_CLK_SOURCE_SETTING
  42. #ifndef CLK_CFG_4
  43. #define CLK_CFG_4 0x10000080
  44. #endif
  45. #ifndef CLK_CFG_4_SET
  46. #define CLK_CFG_4_SET 0x10000084
  47. #endif
  48. #ifndef CLK_CFG_4_CLR
  49. #define CLK_CFG_4_CLR 0x10000088
  50. #endif
  51. #define PWM_MSG(fmt, arg...) DISPMSG("[PWM] " fmt "\n", ##arg)
  52. #define PWM_DBG(fmt, arg...) DISPDBG("[PWM] " fmt "\n", ##arg)
  53. #define PWM_REG_SET(reg, value) do { \
  54. mt65xx_reg_sync_writel(value, (volatile unsigned int*)(reg)); \
  55. PWM_DBG("set reg[0x%08x] = 0x%08x", (unsigned int)reg, (unsigned int)value); \
  56. } while (0)
  57. #define PWM_REG_GET(reg) *((volatile unsigned int*)reg)
  58. #define PWM_DEFAULT_DIV_VALUE 0x0
  59. #define pwm_get_reg_base(id) ((id) == DISP_PWM0 ? DISPSYS_PWM0_BASE : DISPSYS_PWM0_BASE)
  60. #define index_of_pwm(id) ((id == DISP_PWM0) ? 0 : 1)
  61. static int g_pwm_inited = 0;
  62. static disp_pwm_id_t g_pwm_main_id = DISP_PWM0;
  63. void disp_pwm_init(disp_pwm_id_t id)
  64. {
  65. struct cust_mt65xx_led *cust_led_list;
  66. struct cust_mt65xx_led *cust;
  67. struct PWM_config *config_data;
  68. unsigned int pwm_div;
  69. unsigned int reg_base = pwm_get_reg_base(id);
  70. if (g_pwm_inited & id)
  71. return;
  72. if (id & DISP_PWM0)
  73. ddp_enable_module_clock(DISP_MODULE_PWM0);
  74. if (id & DISP_PWM1)
  75. ASSERT(0);
  76. pwm_div = PWM_DEFAULT_DIV_VALUE;
  77. cust_led_list = get_cust_led_list();
  78. if (cust_led_list) {
  79. /* WARNING: may overflow if MT65XX_LED_TYPE_LCD not configured properly */
  80. cust = &cust_led_list[MT65XX_LED_TYPE_LCD];
  81. if ((strcmp(cust->name,"lcd-backlight") == 0) && (cust->mode == MT65XX_LED_MODE_CUST_BLS_PWM)) {
  82. config_data = &cust->config_data;
  83. if (config_data->clock_source >= 0 && config_data->clock_source <= 3) {
  84. unsigned int reg_val = PWM_REG_GET(CLK_CFG_4);
  85. PWM_REG_SET(CLK_CFG_4_CLR, 0x3000000);
  86. PWM_REG_SET(CLK_CFG_4_SET, (0x3000000 & (config_data->clock_source << 24)));
  87. PWM_DBG("disp_pwm_init : CLK_CFG_4 0x%x => 0x%x", reg_val, PWM_REG_GET(CLK_CFG_4));
  88. }
  89. /* Some backlight chip/PMIC(e.g. MT6332) only accept slower clock */
  90. pwm_div = (config_data->div == 0) ? PWM_DEFAULT_DIV_VALUE : config_data->div;
  91. pwm_div &= 0x3FF;
  92. PWM_MSG("disp_pwm_init : PWM config data (%d,%d)", 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_DBG("reg[0x%08x] = 0x%08x", (reg_base + offset), PWM_REG_GET(reg_base + offset));
  160. }
  161. return 0;
  162. }