mtk_tc_D3.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. #include <linux/version.h>
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #include <linux/dmi.h>
  5. #include <linux/acpi.h>
  6. #include <linux/thermal.h>
  7. #include <linux/platform_device.h>
  8. #include <mt-plat/aee.h>
  9. #include <linux/types.h>
  10. #include <linux/delay.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/spinlock.h>
  13. #include <mt-plat/sync_write.h>
  14. #include "mt-plat/mtk_thermal_monitor.h"
  15. #include <linux/seq_file.h>
  16. #include <linux/slab.h>
  17. #include "mtk_thermal_typedefs.h"
  18. #include "mach/mt_thermal.h"
  19. #include "mt_gpufreq.h"
  20. #if defined(CONFIG_MTK_CLKMGR)
  21. #include <mach/mt_clkmgr.h>
  22. #else
  23. #include <linux/clk.h>
  24. #endif
  25. #include <mt_spm.h>
  26. #include <mt_ptp.h>
  27. #include <mach/wd_api.h>
  28. #include <mtk_gpu_utility.h>
  29. #include <linux/time.h>
  30. #include <mach/mt_clkmgr.h>
  31. #define __MT_MTK_TS_CPU_C__
  32. #include <tscpu_settings.h>
  33. /* 1: turn on RT kthread for thermal protection in this sw module; 0: turn off */
  34. #if MTK_TS_CPU_RT
  35. #include <linux/sched.h>
  36. #include <linux/kthread.h>
  37. #endif
  38. #ifdef CONFIG_OF
  39. #include <linux/of.h>
  40. #include <linux/of_irq.h>
  41. #include <linux/of_address.h>
  42. #endif
  43. #define __MT_MTK_TS_CPU_C__
  44. #include <mt-plat/mt_devinfo.h>
  45. /*=============================================================
  46. *Local variable definition
  47. *=============================================================*/
  48. /*
  49. Bank0 : CPU (TS_MCU2)
  50. Bank1 : SOC+GPU (TS_MCU2, TS_MCU1)
  51. Bank2 : LTE (TS_MCU2, TS_ABB)
  52. TS_ABB => TS_MCU3
  53. */
  54. int tscpu_bank_ts[THERMAL_BANK_NUM][ENUM_MAX];
  55. int tscpu_bank_ts_r[THERMAL_BANK_NUM][ENUM_MAX];
  56. /* chip dependent */
  57. bank_t tscpu_g_bank[THERMAL_BANK_NUM] = {
  58. [0] = {
  59. .ts = {TS_FILL(MCU2)},
  60. .ts_number = 1},
  61. [1] = {
  62. .ts = {TS_FILL(MCU2), TS_FILL(MCU1)},
  63. .ts_number = 2},
  64. [2] = {
  65. .ts = {TS_FILL(MCU2), TS_FILL(MCU3)},
  66. .ts_number = 2},
  67. };
  68. #ifdef CONFIG_OF
  69. const struct of_device_id mt_thermal_of_match[2] = {
  70. {.compatible = "mediatek,THERM_CTRL",},
  71. {},
  72. };
  73. #endif
  74. int tscpu_debug_log = 0;
  75. #if MTK_TS_CPU_RT
  76. static struct task_struct *ktp_thread_handle;
  77. #endif
  78. static S32 g_adc_ge_t;
  79. static S32 g_adc_oe_t;
  80. static S32 g_o_vtsmcu1;
  81. static S32 g_o_vtsmcu2;
  82. static S32 g_o_vtsabb;
  83. static S32 g_degc_cali;
  84. static S32 g_adc_cali_en_t;
  85. static S32 g_o_slope;
  86. static S32 g_o_slope_sign;
  87. static S32 g_id;
  88. static S32 g_ge = 1;
  89. static S32 g_oe = 1;
  90. static S32 g_gain = 1;
  91. static S32 g_x_roomt[THERMAL_SENSOR_NUM] = { 0 };
  92. static U32 calefuse1;
  93. static U32 calefuse2;
  94. static U32 calefuse3;
  95. /**
  96. * If curr_temp >= tscpu_polling_trip_temp1, use interval
  97. * else if cur_temp >= tscpu_polling_trip_temp2 && curr_temp < tscpu_polling_trip_temp1,
  98. * use interval*tscpu_polling_factor1
  99. * else, use interval*tscpu_polling_factor2
  100. */
  101. /* chip dependent */
  102. int tscpu_polling_trip_temp1 = 30000;
  103. int tscpu_polling_trip_temp2 = 20000;
  104. int tscpu_polling_factor1 = 1;
  105. int tscpu_polling_factor2 = 2;
  106. #if MTKTSCPU_FAST_POLLING
  107. /* Combined fast_polling_trip_temp and fast_polling_factor,
  108. it means polling_delay will be 1/5 of original interval
  109. after mtktscpu reports > 65C w/o exit point */
  110. int fast_polling_trip_temp = 70000;
  111. int fast_polling_factor = 2;
  112. int tscpu_cur_fp_factor = 1;
  113. int tscpu_next_fp_factor = 1;
  114. #endif
  115. /*=============================================================
  116. * Local function declartation
  117. *=============================================================*/
  118. static S32 temperature_to_raw_room(U32 ret);
  119. static void set_tc_trigger_hw_protect(int temperature, int temperature2);
  120. /*=============================================================
  121. *Weak functions
  122. *=============================================================*/
  123. void __attribute__ ((weak))
  124. mt_ptp_lock(unsigned long *flags)
  125. {
  126. pr_err("E_WF: %s doesn't exist\n", __func__);
  127. }
  128. void __attribute__ ((weak))
  129. mt_ptp_unlock(unsigned long *flags)
  130. {
  131. pr_err("E_WF: %s doesn't exist\n", __func__);
  132. }
  133. /*=============================================================*/
  134. /* chip dependent */
  135. int tscpu_thermal_clock_on(void)
  136. {
  137. int ret = -1;
  138. tscpu_printk("tscpu_thermal_clock_on\n");
  139. #if defined(CONFIG_MTK_CLKMGR)
  140. ret = enable_clock(MT_CG_PERI_THERM, "THERMAL");
  141. #else
  142. tscpu_printk("CCF_thermal_clock_on\n");
  143. ret = clk_prepare_enable(therm_main);
  144. if (ret)
  145. tscpu_printk("Cannot enable thermal clock.\n");
  146. #endif
  147. return ret;
  148. }
  149. /* chip dependent */
  150. int tscpu_thermal_clock_off(void)
  151. {
  152. int ret = -1;
  153. tscpu_dprintk("tscpu_thermal_clock_off\n");
  154. #if defined(CONFIG_MTK_CLKMGR)
  155. ret = disable_clock(MT_CG_PERI_THERM, "THERMAL");
  156. #else
  157. tscpu_printk("CCF_thermal_clock_off\n");
  158. clk_disable_unprepare(therm_main);
  159. #endif
  160. return ret;
  161. }
  162. #if 0
  163. static void get_thermal_all_register(void)
  164. {
  165. tscpu_dprintk("get_thermal_all_register\n");
  166. tscpu_dprintk("TEMPMSR1 = 0x%8x\n", DRV_Reg32(TEMPMSR1));
  167. tscpu_dprintk("TEMPMSR2 = 0x%8x\n", DRV_Reg32(TEMPMSR2));
  168. tscpu_dprintk("TEMPMONCTL0 = 0x%8x\n", DRV_Reg32(TEMPMONCTL0));
  169. tscpu_dprintk("TEMPMONCTL1 = 0x%8x\n", DRV_Reg32(TEMPMONCTL1));
  170. tscpu_dprintk("TEMPMONCTL2 = 0x%8x\n", DRV_Reg32(TEMPMONCTL2));
  171. tscpu_dprintk("TEMPMONINT = 0x%8x\n", DRV_Reg32(TEMPMONINT));
  172. tscpu_dprintk("TEMPMONINTSTS = 0x%8x\n", DRV_Reg32(TEMPMONINTSTS));
  173. tscpu_dprintk("TEMPMONIDET0 = 0x%8x\n", DRV_Reg32(TEMPMONIDET0));
  174. tscpu_dprintk("TEMPMONIDET1 = 0x%8x\n", DRV_Reg32(TEMPMONIDET1));
  175. tscpu_dprintk("TEMPMONIDET2 = 0x%8x\n", DRV_Reg32(TEMPMONIDET2));
  176. tscpu_dprintk("TEMPH2NTHRE = 0x%8x\n", DRV_Reg32(TEMPH2NTHRE));
  177. tscpu_dprintk("TEMPHTHRE = 0x%8x\n", DRV_Reg32(TEMPHTHRE));
  178. tscpu_dprintk("TEMPCTHRE = 0x%8x\n", DRV_Reg32(TEMPCTHRE));
  179. tscpu_dprintk("TEMPOFFSETH = 0x%8x\n", DRV_Reg32(TEMPOFFSETH));
  180. tscpu_dprintk("TEMPOFFSETL = 0x%8x\n", DRV_Reg32(TEMPOFFSETL));
  181. tscpu_dprintk("TEMPMSRCTL0 = 0x%8x\n", DRV_Reg32(TEMPMSRCTL0));
  182. tscpu_dprintk("TEMPMSRCTL1 = 0x%8x\n", DRV_Reg32(TEMPMSRCTL1));
  183. tscpu_dprintk("TEMPAHBPOLL = 0x%8x\n", DRV_Reg32(TEMPAHBPOLL));
  184. tscpu_dprintk("TEMPAHBTO = 0x%8x\n", DRV_Reg32(TEMPAHBTO));
  185. tscpu_dprintk("TEMPADCPNP0 = 0x%8x\n", DRV_Reg32(TEMPADCPNP0));
  186. tscpu_dprintk("TEMPADCPNP1 = 0x%8x\n", DRV_Reg32(TEMPADCPNP1));
  187. tscpu_dprintk("TEMPADCPNP2 = 0x%8x\n", DRV_Reg32(TEMPADCPNP2));
  188. tscpu_dprintk("TEMPADCMUX = 0x%8x\n", DRV_Reg32(TEMPADCMUX));
  189. tscpu_dprintk("TEMPADCEXT = 0x%8x\n", DRV_Reg32(TEMPADCEXT));
  190. tscpu_dprintk("TEMPADCEXT1 = 0x%8x\n", DRV_Reg32(TEMPADCEXT1));
  191. tscpu_dprintk("TEMPADCEN = 0x%8x\n", DRV_Reg32(TEMPADCEN));
  192. tscpu_dprintk("TEMPPNPMUXADDR = 0x%8x\n", DRV_Reg32(TEMPPNPMUXADDR));
  193. tscpu_dprintk("TEMPADCMUXADDR = 0x%8x\n", DRV_Reg32(TEMPADCMUXADDR));
  194. tscpu_dprintk("TEMPADCEXTADDR = 0x%8x\n", DRV_Reg32(TEMPADCEXTADDR));
  195. tscpu_dprintk("TEMPADCEXT1ADDR = 0x%8x\n", DRV_Reg32(TEMPADCEXT1ADDR));
  196. tscpu_dprintk("TEMPADCENADDR = 0x%8x\n", DRV_Reg32(TEMPADCENADDR));
  197. tscpu_dprintk("TEMPADCVALIDADDR = 0x%8x\n", DRV_Reg32(TEMPADCVALIDADDR));
  198. tscpu_dprintk("TEMPADCVOLTADDR = 0x%8x\n", DRV_Reg32(TEMPADCVOLTADDR));
  199. tscpu_dprintk("TEMPRDCTRL = 0x%8x\n", DRV_Reg32(TEMPRDCTRL));
  200. tscpu_dprintk("TEMPADCVALIDMASK = 0x%8x\n", DRV_Reg32(TEMPADCVALIDMASK));
  201. tscpu_dprintk("TEMPADCVOLTAGESHIFT = 0x%8x\n", DRV_Reg32(TEMPADCVOLTAGESHIFT));
  202. tscpu_dprintk("TEMPADCWRITECTRL = 0x%8x\n", DRV_Reg32(TEMPADCWRITECTRL));
  203. tscpu_dprintk("TEMPMSR0 = 0x%8x\n", DRV_Reg32(TEMPMSR0));
  204. tscpu_dprintk("TEMPIMMD0 = 0x%8x\n", DRV_Reg32(TEMPIMMD0));
  205. tscpu_dprintk("TEMPIMMD1 = 0x%8x\n", DRV_Reg32(TEMPIMMD1));
  206. tscpu_dprintk("TEMPIMMD2 = 0x%8x\n", DRV_Reg32(TEMPIMMD2));
  207. tscpu_dprintk("TEMPPROTCTL = 0x%8x\n", DRV_Reg32(TEMPPROTCTL));
  208. tscpu_dprintk("TEMPPROTTA = 0x%8x\n", DRV_Reg32(TEMPPROTTA));
  209. tscpu_dprintk("TEMPPROTTB = 0x%8x\n", DRV_Reg32(TEMPPROTTB));
  210. tscpu_dprintk("TEMPPROTTC = 0x%8x\n", DRV_Reg32(TEMPPROTTC));
  211. tscpu_dprintk("TEMPSPARE0 = 0x%8x\n", DRV_Reg32(TEMPSPARE0));
  212. tscpu_dprintk("TEMPSPARE1 = 0x%8x\n", DRV_Reg32(TEMPSPARE1));
  213. tscpu_dprintk("TEMPSPARE2 = 0x%8x\n", DRV_Reg32(TEMPSPARE2));
  214. tscpu_dprintk("TEMPSPARE3 = 0x%8x\n", DRV_Reg32(TEMPSPARE3));
  215. /* tscpu_dprintk("0x11001040 = 0x%8x\n", DRV_Reg32(0xF1001040)); */
  216. }
  217. #endif
  218. /* TODO: FIXME */
  219. void get_thermal_slope_intercept(struct TS_PTPOD *ts_info, thermal_bank_name ts_bank)
  220. {
  221. unsigned int temp0, temp1, temp2;
  222. struct TS_PTPOD ts_ptpod;
  223. S32 x_roomt;
  224. tscpu_dprintk("get_thermal_slope_intercept\n");
  225. /*
  226. Bank0 : CPU (TS_MCU2)
  227. Bank1 : SOC+GPU (TS_MCU2, TS_MCU1)
  228. Bank2 : LTE (TS_MCU2, TS_ABB)
  229. 1->0 2->1 ABB->2
  230. */
  231. /*
  232. If there are two or more sensors in a bank, choose the sensor calibration value of
  233. the dominant sensor. You can observe it in the thermal doc provided by Thermal DE.
  234. For example,
  235. Bank 1 is for SOC + GPU. Observe all scenarios related to GPU tests to
  236. determine which sensor is the highest temperature in all tests.
  237. Then, It is the dominant sensor.
  238. (Confirmed by Thermal DE Alfred Tsai)
  239. */
  240. /* chip dependent */
  241. switch (ts_bank) {
  242. case THERMAL_BANK0: /* CPU (TS_MCU2) */
  243. x_roomt = g_x_roomt[1];
  244. break;
  245. case THERMAL_BANK1: /* GPU (TS_MCU2, TS_MCU1) */
  246. x_roomt = MAX(g_x_roomt[1], g_x_roomt[0]);
  247. break;
  248. case THERMAL_BANK2: /* LTE (TS_MCU2, TS_ABB) */
  249. x_roomt = MAX(g_x_roomt[1], g_x_roomt[2]);
  250. break;
  251. default: /* choose high temp */
  252. x_roomt = MAX(g_x_roomt[1], g_x_roomt[0]);
  253. break;
  254. }
  255. /*
  256. The equations in this function are confirmed by Thermal DE Alfred Tsai.
  257. Don't have to change until using next generation thermal sensors.
  258. */
  259. temp0 = (10000 * 100000 / g_gain) * 15 / 18;
  260. if (g_o_slope_sign == 0)
  261. temp1 = temp0 / (165 + g_o_slope);
  262. else
  263. temp1 = temp0 / (165 - g_o_slope);
  264. ts_ptpod.ts_MTS = temp1;
  265. temp0 = (g_degc_cali * 10 / 2);
  266. temp1 = ((10000 * 100000 / 4096 / g_gain) * g_oe + x_roomt * 10) * 15 / 18;
  267. if (g_o_slope_sign == 0)
  268. temp2 = temp1 * 10 / (165 + g_o_slope);
  269. else
  270. temp2 = temp1 * 10 / (165 - g_o_slope);
  271. ts_ptpod.ts_BTS = (temp0 + temp2 - 250) * 4 / 10;
  272. ts_info->ts_MTS = ts_ptpod.ts_MTS;
  273. ts_info->ts_BTS = ts_ptpod.ts_BTS;
  274. tscpu_printk("ts_MTS=%d, ts_BTS=%d\n", ts_ptpod.ts_MTS, ts_ptpod.ts_BTS);
  275. }
  276. EXPORT_SYMBOL(get_thermal_slope_intercept);
  277. /* chip dependent */
  278. void mtkts_dump_cali_info(void)
  279. {
  280. tscpu_printk("[cal] g_adc_ge_t = 0x%x\n", g_adc_ge_t);
  281. tscpu_printk("[cal] g_adc_oe_t = 0x%x\n", g_adc_oe_t);
  282. tscpu_printk("[cal] g_degc_cali = 0x%x\n", g_degc_cali);
  283. tscpu_printk("[cal] g_adc_cali_en_t = 0x%x\n", g_adc_cali_en_t);
  284. tscpu_printk("[cal] g_o_slope = 0x%x\n", g_o_slope);
  285. tscpu_printk("[cal] g_o_slope_sign = 0x%x\n", g_o_slope_sign);
  286. tscpu_printk("[cal] g_id = 0x%x\n", g_id);
  287. tscpu_printk("[cal] g_o_vtsmcu2 = 0x%x\n", g_o_vtsmcu2);
  288. tscpu_printk("[cal] g_o_vtsabb = 0x%x\n", g_o_vtsabb);
  289. }
  290. void tscpu_thermal_cal_prepare(void)
  291. {
  292. U32 temp0 = 0, temp1 = 0, temp2 = 0;
  293. temp0 = get_devinfo_with_index(ADDRESS_INDEX_0);
  294. temp1 = get_devinfo_with_index(ADDRESS_INDEX_1);
  295. temp2 = get_devinfo_with_index(ADDRESS_INDEX_2);
  296. pr_debug("[calibration] temp0=0x%x, temp1=0x%x, temp2=0x%x\n", temp0, temp1, temp2);
  297. /* chip dependent */
  298. g_adc_ge_t = ((temp0 & 0xFFC00000) >> 22); /* ADC_GE_T [9:0] *(0x102061A0)[31:22] */
  299. g_adc_oe_t = ((temp0 & 0x003FF000) >> 12); /* ADC_OE_T [9:0] *(0x102061A0)[21:12] */
  300. g_o_vtsmcu1 = (temp1 & 0x03FE0000) >> 17; /* O_VTSMCU1 (9b) *(0x1020619C)[25:17] */
  301. g_o_vtsmcu2 = (temp1 & 0x0001FF00) >> 8; /* O_VTSMCU2 (9b) *(0x1020619C)[16:8] */
  302. g_o_vtsabb = (temp2 & 0x007FC000) >> 14; /* O_VTSABB (9b) *(0x102061A4)[22:14] */
  303. g_degc_cali = (temp1 & 0x0000007E) >> 1; /* DEGC_cali (6b) *(0x1020619C)[6:1] */
  304. g_adc_cali_en_t = (temp1 & 0x00000001); /* ADC_CALI_EN_T(1b) *(0x1020619C)[0] */
  305. g_o_slope_sign = (temp1 & 0x00000080) >> 7; /* O_SLOPE_SIGN (1b) *(0x1020619C)[7] */
  306. g_o_slope = (temp1 & 0xFC000000) >> 26; /* O_SLOPE (6b) *(0x1020619C)[31:26] */
  307. g_id = (temp0 & 0x00000200) >> 9; /* ID (1b) *(0x102061A0)[9] */
  308. /*
  309. Check ID bit
  310. If ID=0 (TSMC sample) , ignore O_SLOPE EFuse value and set O_SLOPE=0.
  311. If ID=1 (non-TSMC sample), read O_SLOPE EFuse value for following calculation.
  312. */
  313. if (g_id == 0)
  314. g_o_slope = 0;
  315. /* g_adc_cali_en_t=0;//test only */
  316. if (g_adc_cali_en_t == 1) {
  317. /* thermal_enable = true; */
  318. } else {
  319. tscpu_printk("This sample is not Thermal calibrated\n");
  320. g_adc_ge_t = 512;
  321. g_adc_oe_t = 512;
  322. g_degc_cali = 40;
  323. g_o_slope = 0;
  324. g_o_slope_sign = 0;
  325. g_o_vtsmcu1 = 260;
  326. g_o_vtsmcu2 = 260;
  327. g_o_vtsabb = 260;
  328. }
  329. mtkts_dump_cali_info();
  330. }
  331. void tscpu_thermal_cal_prepare_2(U32 ret)
  332. {
  333. S32 format_1 = 0, format_2 = 0, format_3 = 0;
  334. /* tscpu_printk("tscpu_thermal_cal_prepare_2\n"); */
  335. g_ge = ((g_adc_ge_t - 512) * 10000) / 4096; /* ge * 10000 */
  336. g_oe = (g_adc_oe_t - 512);
  337. g_gain = (10000 + g_ge);
  338. format_1 = (g_o_vtsmcu1 + 3350 - g_oe);
  339. format_2 = (g_o_vtsmcu2 + 3350 - g_oe);
  340. format_3 = (g_o_vtsabb + 3350 - g_oe);
  341. g_x_roomt[0] = (((format_1 * 10000) / 4096) * 10000) / g_gain; /* g_x_roomt1 * 10000 */
  342. g_x_roomt[1] = (((format_2 * 10000) / 4096) * 10000) / g_gain; /* g_x_roomt2 * 10000 */
  343. g_x_roomt[2] = (((format_3 * 10000) / 4096) * 10000) / g_gain; /* g_x_roomt3 * 10000 */
  344. /*
  345. tscpu_printk("[cal] g_ge = 0x%x\n",g_ge);
  346. tscpu_printk("[cal] g_gain = 0x%x\n",g_gain);
  347. tscpu_printk("[cal] g_x_roomt1 = 0x%x\n",g_x_roomt[0]);
  348. tscpu_printk("[cal] g_x_roomt2 = 0x%x\n",g_x_roomt[1]);
  349. tscpu_printk("[cal] g_x_roomt3 = 0x%x\n",g_x_roomt[2]);
  350. */
  351. }
  352. #if THERMAL_CONTROLLER_HW_TP
  353. static S32 temperature_to_raw_room(U32 ret)
  354. {
  355. /* Ycurr = [(Tcurr - DEGC_cali/2)*(165+O_slope)*(18/15)*(1/10000)+X_roomtabb]*Gain*4096 + OE */
  356. S32 t_curr = ret;
  357. S32 format_1 = 0;
  358. S32 format_2 = 0;
  359. S32 format_3[THERMAL_SENSOR_NUM] = { 0 };
  360. S32 format_4[THERMAL_SENSOR_NUM] = { 0 };
  361. S32 i, index = 0, temp = 0;
  362. /* tscpu_dprintk("temperature_to_raw_room\n"); */
  363. if (g_o_slope_sign == 0) { /* O_SLOPE is Positive. */
  364. format_1 = t_curr - (g_degc_cali * 1000 / 2);
  365. format_2 = format_1 * (165 + g_o_slope) * 18 / 15;
  366. format_2 = format_2 - 2 * format_2;
  367. for (i = 0; i < THERMAL_SENSOR_NUM; i++) {
  368. format_3[i] = format_2 / 1000 + g_x_roomt[i] * 10;
  369. format_4[i] = (format_3[i] * 4096 / 10000 * g_gain) / 100000 + g_oe;
  370. }
  371. } else { /* O_SLOPE is Negative. */
  372. format_1 = t_curr - (g_degc_cali * 1000 / 2);
  373. format_2 = format_1 * (165 - g_o_slope) * 18 / 15;
  374. format_2 = format_2 - 2 * format_2;
  375. for (i = 0; i < THERMAL_SENSOR_NUM; i++) {
  376. format_3[i] = format_2 / 1000 + g_x_roomt[i] * 10;
  377. format_4[i] = (format_3[i] * 4096 / 10000 * g_gain) / 100000 + g_oe;
  378. }
  379. }
  380. temp = 0;
  381. for (i = 0; i < THERMAL_SENSOR_NUM; i++) {
  382. if (temp < format_4[i]) {
  383. temp = format_4[i];
  384. index = i;
  385. }
  386. }
  387. /* tscpu_dprintk("[Temperature_to_raw_roomt] temperature=%d, raw[%d]=%d", ret, index, format_4[index]); */
  388. return format_4[index];
  389. }
  390. #endif
  391. static S32 raw_to_temperature_roomt(U32 ret, thermal_sensor_name ts_name)
  392. {
  393. S32 t_current = 0;
  394. S32 y_curr = ret;
  395. S32 format_1 = 0;
  396. S32 format_2 = 0;
  397. S32 format_3 = 0;
  398. S32 format_4 = 0;
  399. S32 xtoomt = 0;
  400. xtoomt = g_x_roomt[ts_name];
  401. /* tscpu_dprintk("raw_to_temperature_room,ts_num=%d,xtoomt=%d\n",ts_name,xtoomt); */
  402. if (ret == 0)
  403. return 0;
  404. format_1 = ((g_degc_cali * 10) >> 1);
  405. format_2 = (y_curr - g_oe);
  406. format_3 = (((((format_2) * 10000) >> 12) * 10000) / g_gain) - xtoomt;
  407. format_3 = format_3 * 15 / 18;
  408. if (g_o_slope_sign == 0)
  409. format_4 = ((format_3 * 100) / (165 + g_o_slope)); /* uint = 0.1 deg */
  410. else
  411. format_4 = ((format_3 * 100) / (165 - g_o_slope)); /* uint = 0.1 deg */
  412. format_4 = format_4 - (format_4 << 1);
  413. t_current = format_1 + format_4; /* uint = 0.1 deg */
  414. /* tscpu_dprintk("raw_to_temperature_room,t_current=%d\n",t_current); */
  415. return t_current;
  416. }
  417. /*
  418. Bank0 : CPU (TS_MCU2)
  419. Bank1 : SOC+GPU (TS_MCU2, TS_MCU1)
  420. Bank2 : LTE (TS_MCU2, TS_ABB)
  421. */
  422. /* chip dependent */
  423. int get_immediate_cpu_wrap(void)
  424. {
  425. int curr_temp;
  426. curr_temp = tscpu_bank_ts[THERMAL_BANK0][MCU2];
  427. tscpu_dprintk("get_immediate_cpu_wrap curr_temp=%d\n", curr_temp);
  428. return curr_temp;
  429. }
  430. int get_immediate_gpu_wrap(void)
  431. {
  432. int curr_temp;
  433. curr_temp = MAX(tscpu_bank_ts[THERMAL_BANK1][MCU2], tscpu_bank_ts[THERMAL_BANK1][MCU1]);
  434. tscpu_dprintk("get_immediate_gpu_wrap curr_temp=%d\n", curr_temp);
  435. return curr_temp;
  436. }
  437. int get_immediate_lte_wrap(void)
  438. {
  439. int curr_temp;
  440. curr_temp = MAX(tscpu_bank_ts[THERMAL_BANK2][MCU2], tscpu_bank_ts[THERMAL_BANK2][MCU3]);
  441. tscpu_dprintk("get_immediate_soc_wrap curr_temp=%d\n", curr_temp);
  442. return curr_temp;
  443. }
  444. /*
  445. Bank0 : CPU (TS_MCU2)
  446. Bank1 : SOC+GPU (TS_MCU2, TS_MCU1)
  447. Bank2 : LTE (TS_MCU2, TS_ABB)
  448. */
  449. /* chip dependent */
  450. int get_immediate_ts1_wrap(void)
  451. {
  452. int curr_temp;
  453. /* curr_temp = GPU_TS_MCU1_T; */
  454. curr_temp = tscpu_bank_ts[THERMAL_BANK1][MCU1];
  455. tscpu_dprintk("get_immediate_ts1_wrap curr_temp=%d\n", curr_temp);
  456. return curr_temp;
  457. }
  458. int get_immediate_ts2_wrap(void)
  459. {
  460. int curr_temp;
  461. /* curr_temp = CPU_TS_MCU2_T; */
  462. curr_temp = tscpu_bank_ts[THERMAL_BANK0][MCU2];
  463. tscpu_dprintk("get_immediate_ts2_wrap curr_temp=%d\n", curr_temp);
  464. return curr_temp;
  465. }
  466. int get_immediate_ts3_wrap(void)
  467. {
  468. int curr_temp;
  469. /* curr_temp = LTE_TS_MCU3_T; */
  470. curr_temp = tscpu_bank_ts[THERMAL_BANK2][MCU3];
  471. tscpu_dprintk("get_immediate_ts3_wrap curr_temp=%d\n", curr_temp);
  472. return curr_temp;
  473. }
  474. static void thermal_interrupt_handler(int bank)
  475. {
  476. U32 ret = 0;
  477. unsigned long flags;
  478. mt_ptp_lock(&flags);
  479. tscpu_switch_bank(bank);
  480. ret = DRV_Reg32(TEMPMONINTSTS);
  481. /* pr_debug("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); */
  482. tscpu_printk("thermal_interrupt_handler,bank=0x%08x,ret=0x%08x\n", bank, ret);
  483. /* pr_debug("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); */
  484. /* ret2 = DRV_Reg32(THERMINTST); */
  485. /* pr_debug("thermal_interrupt_handler : THERMINTST = 0x%x\n", ret2); */
  486. /* for SPM reset debug */
  487. /* dump_spm_reg(); */
  488. /* tscpu_printk("thermal_isr: [Interrupt trigger]: status = 0x%x\n", ret); */
  489. if (ret & THERMAL_MON_CINTSTS0)
  490. tscpu_printk("thermal_isr: thermal sensor point 0 - cold interrupt trigger\n");
  491. if (ret & THERMAL_MON_HINTSTS0)
  492. tscpu_printk("<<<thermal_isr>>>: thermal sensor point 0 - hot interrupt trigger\n");
  493. if (ret & THERMAL_MON_HINTSTS1)
  494. tscpu_printk("<<<thermal_isr>>>: thermal sensor point 1 - hot interrupt trigger\n");
  495. if (ret & THERMAL_MON_HINTSTS2)
  496. tscpu_printk("<<<thermal_isr>>>: thermal sensor point 2 - hot interrupt trigger\n");
  497. if (ret & THERMAL_tri_SPM_State0)
  498. tscpu_printk("thermal_isr: Thermal state0 to trigger SPM state0\n");
  499. if (ret & THERMAL_tri_SPM_State1) {
  500. /* tscpu_printk("thermal_isr: Thermal state1 to trigger SPM state1\n"); */
  501. #if MTK_TS_CPU_RT
  502. /* tscpu_printk("THERMAL_tri_SPM_State1, T=%d,%d,%d\n", CPU_TS_MCU2_T, GPU_TS_MCU1_T, LTE_TS_MCU3_T); */
  503. wake_up_process(ktp_thread_handle);
  504. #endif
  505. }
  506. if (ret & THERMAL_tri_SPM_State2)
  507. tscpu_printk("thermal_isr: Thermal state2 to trigger SPM state2\n");
  508. mt_ptp_unlock(&flags);
  509. }
  510. irqreturn_t tscpu_thermal_all_bank_interrupt_handler(int irq, void *dev_id)
  511. {
  512. U32 ret = 0, i, mask = 1;
  513. ret = DRV_Reg32(THERMINTST);
  514. ret = ret & 0xF;
  515. pr_debug("thermal_interrupt_handler : THERMINTST = 0x%x\n", ret);
  516. for (i = 0; i < TS_LEN_ARRAY(tscpu_g_bank); i++) {
  517. mask = 1 << i;
  518. if ((ret & mask) == 0)
  519. thermal_interrupt_handler(i);
  520. }
  521. return IRQ_HANDLED;
  522. }
  523. static void thermal_reset_and_initial(void)
  524. {
  525. /* pr_debug( "thermal_reset_and_initial\n"); */
  526. /* tscpu_thermal_clock_on(); */
  527. /* Calculating period unit in Module clock x 256, and the Module clock */
  528. /* will be changed to 26M when Infrasys enters Sleep mode. */
  529. /* THERMAL_WRAP_WR32(0x000003FF, TEMPMONCTL1); // counting unit is 1023 * 15.15ns ~ 15.5us */
  530. /* bus clock 66M counting unit is 4*15.15ns* 256 = 15513.6 ms=15.5us */
  531. /* THERMAL_WRAP_WR32(0x00000004, TEMPMONCTL1);*/
  532. /* bus clock 66M counting unit is 12*15.15ns* 256 = 46.540us */
  533. THERMAL_WRAP_WR32(0x0000000C, TEMPMONCTL1);
  534. /*bus clock 66M counting unit is 4*15.15ns* 256 = 15513.6 ms=15.5us */
  535. /* THERMAL_WRAP_WR32(0x000001FF, TEMPMONCTL1);*/
  536. #if THERMAL_CONTROLLER_HW_FILTER == 2
  537. THERMAL_WRAP_WR32(0x07E007E0, TEMPMONCTL2); /* both filt and sen interval is 2016*15.5us = 31.25ms */
  538. THERMAL_WRAP_WR32(0x001F7972, TEMPAHBPOLL); /* poll is set to 31.25ms */
  539. THERMAL_WRAP_WR32(0x00000049, TEMPMSRCTL0); /* temperature sampling control, 2 out of 4 samples */
  540. #elif THERMAL_CONTROLLER_HW_FILTER == 4
  541. THERMAL_WRAP_WR32(0x050A050A, TEMPMONCTL2); /* both filt and sen interval is 20ms */
  542. THERMAL_WRAP_WR32(0x001424C4, TEMPAHBPOLL); /* poll is set to 20ms */
  543. THERMAL_WRAP_WR32(0x000000DB, TEMPMSRCTL0); /* temperature sampling control, 4 out of 6 samples */
  544. #elif THERMAL_CONTROLLER_HW_FILTER == 8
  545. THERMAL_WRAP_WR32(0x03390339, TEMPMONCTL2); /* both filt and sen interval is 12.5ms */
  546. THERMAL_WRAP_WR32(0x000C96FA, TEMPAHBPOLL); /* poll is set to 12.5ms */
  547. THERMAL_WRAP_WR32(0x00000124, TEMPMSRCTL0); /* temperature sampling control, 8 out of 10 samples */
  548. #elif THERMAL_CONTROLLER_HW_FILTER == 16
  549. THERMAL_WRAP_WR32(0x01C001C0, TEMPMONCTL2); /* both filt and sen interval is 6.94ms */
  550. THERMAL_WRAP_WR32(0x0006FE8B, TEMPAHBPOLL); /* poll is set to 458379*15.15= 6.94ms */
  551. THERMAL_WRAP_WR32(0x0000016D, TEMPMSRCTL0); /* temperature sampling control, 16 out of 18 samples */
  552. #else /* default 1 */
  553. /* filt interval is 1 * 46.540us = 46.54us, sen interval is 429 * 46.540us = 19.96ms */
  554. THERMAL_WRAP_WR32(0x000101AD, TEMPMONCTL2);
  555. /* filt interval is 1 * 46.540us = 46.54us, sen interval is 858 * 46.540us = 39.93ms */
  556. /* THERMAL_WRAP_WR32(0x0001035A, TEMPMONCTL2);
  557. * filt interval is 1 * 46.540us = 46.54us, sen interval is 1287 * 46.540us = 59.89 ms */
  558. /* THERMAL_WRAP_WR32(0x00010507, TEMPMONCTL2);*/
  559. /* THERMAL_WRAP_WR32(0x00000001, TEMPAHBPOLL); // poll is set to 1 * 46.540us = 46.540us */
  560. THERMAL_WRAP_WR32(0x00000300, TEMPAHBPOLL); /* poll is set to 10u */
  561. THERMAL_WRAP_WR32(0x00000000, TEMPMSRCTL0); /* temperature sampling control, 1 sample */
  562. #endif
  563. THERMAL_WRAP_WR32(0xFFFFFFFF, TEMPAHBTO); /* exceed this polling time, IRQ would be inserted */
  564. THERMAL_WRAP_WR32(0x00000000, TEMPMONIDET0); /* times for interrupt occurrance */
  565. THERMAL_WRAP_WR32(0x00000000, TEMPMONIDET1); /* times for interrupt occurrance */
  566. /* this value will be stored to TEMPPNPMUXADDR (TEMPSPARE0) automatically by hw */
  567. THERMAL_WRAP_WR32(0x800, TEMPADCMUX);
  568. THERMAL_WRAP_WR32((UINT32) AUXADC_CON1_CLR_P, TEMPADCMUXADDR); /* AHB address for auxadc mux selection */
  569. /* THERMAL_WRAP_WR32(0x1100100C, TEMPADCMUXADDR);// AHB address for auxadc mux selection */
  570. THERMAL_WRAP_WR32(0x800, TEMPADCEN); /* AHB value for auxadc enable */
  571. /* AHB address for auxadc enable (channel 0 immediate mode selected) */
  572. THERMAL_WRAP_WR32((UINT32) AUXADC_CON1_SET_P, TEMPADCENADDR);
  573. /* THERMAL_WRAP_WR32(0x11001008, TEMPADCENADDR);
  574. * AHB address for auxadc enable (channel 0 immediate mode selected)
  575. * this value will be stored to TEMPADCENADDR automatically by hw */
  576. THERMAL_WRAP_WR32((UINT32) AUXADC_DAT11_P, TEMPADCVALIDADDR); /* AHB address for auxadc valid bit */
  577. THERMAL_WRAP_WR32((UINT32) AUXADC_DAT11_P, TEMPADCVOLTADDR); /* AHB address for auxadc voltage output */
  578. /* THERMAL_WRAP_WR32(0x11001040, TEMPADCVALIDADDR); // AHB address for auxadc valid bit */
  579. /* THERMAL_WRAP_WR32(0x11001040, TEMPADCVOLTADDR); // AHB address for auxadc voltage output */
  580. THERMAL_WRAP_WR32(0x0, TEMPRDCTRL); /* read valid & voltage are at the same register */
  581. /* indicate where the valid bit is (the 12th bit is valid bit and 1 is valid) */
  582. THERMAL_WRAP_WR32(0x0000002C, TEMPADCVALIDMASK);
  583. THERMAL_WRAP_WR32(0x0, TEMPADCVOLTAGESHIFT); /* do not need to shift */
  584. /* THERMAL_WRAP_WR32(0x2, TEMPADCWRITECTRL); // enable auxadc mux write transaction */
  585. }
  586. /* Refactor */
  587. static int thermal_config_Bank(void)
  588. {
  589. int i, j = 0;
  590. int ret = -1;
  591. UINT32 temp = 0;
  592. /* AuxADC Initialization,ref MT6592_AUXADC.doc // TODO: check this line */
  593. temp = DRV_Reg32(AUXADC_CON0_V); /* Auto set enable for CH11 */
  594. temp &= 0xFFFFF7FF; /* 0: Not AUTOSET mode */
  595. THERMAL_WRAP_WR32(temp, AUXADC_CON0_V); /* disable auxadc channel 11 synchronous mode */
  596. THERMAL_WRAP_WR32(0x800, AUXADC_CON1_CLR_V); /* disable auxadc channel 11 immediate mode */
  597. for (i = 0; i < TS_LEN_ARRAY(tscpu_g_bank); i++) {
  598. ret = tscpu_switch_bank(i);
  599. thermal_reset_and_initial();
  600. for (j = 0; j < tscpu_g_bank[i].ts_number; j++) {
  601. tscpu_dprintk("%s i %d, j %d\n", __func__, i, j);
  602. tscpu_thermal_tempADCPNP(tscpu_thermal_ADCValueOfMcu
  603. (tscpu_g_bank[i].ts[j].type), j);
  604. }
  605. THERMAL_WRAP_WR32(TS_CONFIGURE_P, TEMPPNPMUXADDR);
  606. THERMAL_WRAP_WR32(0x3, TEMPADCWRITECTRL);
  607. }
  608. THERMAL_WRAP_WR32(0x800, AUXADC_CON1_SET_V);
  609. for (i = 0; i < TS_LEN_ARRAY(tscpu_g_bank); i++) {
  610. ret = tscpu_switch_bank(i);
  611. tscpu_thermal_enable_all_periodoc_sensing_point(i);
  612. }
  613. return ret;
  614. }
  615. /**
  616. * temperature2 to set the middle threshold for interrupting CPU. -275000 to disable it.
  617. */
  618. static void set_tc_trigger_hw_protect(int temperature, int temperature2)
  619. {
  620. int temp = 0;
  621. int raw_high, raw_middle, raw_low;
  622. /* temperature2=80000; test only */
  623. tscpu_dprintk("set_tc_trigger_hw_protect t1=%d t2=%d\n", temperature, temperature2);
  624. /* temperature to trigger SPM state2 */
  625. raw_high = temperature_to_raw_room(temperature);
  626. if (temperature2 > -275000)
  627. raw_middle = temperature_to_raw_room(temperature2);
  628. raw_low = temperature_to_raw_room(5000);
  629. temp = DRV_Reg32(TEMPMONINT);
  630. /* tscpu_printk("set_tc_trigger_hw_protect 1 TEMPMONINT:temp=0x%x\n",temp); */
  631. /* THERMAL_WRAP_WR32(temp & 0x1FFFFFFF, TEMPMONINT); // disable trigger SPM interrupt */
  632. THERMAL_WRAP_WR32(temp & 0x00000000, TEMPMONINT); /* disable trigger SPM interrupt */
  633. /* THERMAL_WRAP_WR32(0x60000, TEMPPROTCTL);// set hot to wakeup event control */
  634. /* THERMAL_WRAP_WR32(0x20000, TEMPPROTCTL);// set hot to wakeup event control */
  635. THERMAL_WRAP_WR32(0x20000, TEMPPROTCTL); /* set hot to wakeup event control */
  636. THERMAL_WRAP_WR32(raw_low, TEMPPROTTA);
  637. if (temperature2 > -275000)
  638. THERMAL_WRAP_WR32(raw_middle, TEMPPROTTB); /* register will remain unchanged if -275000... */
  639. THERMAL_WRAP_WR32(raw_high, TEMPPROTTC); /* set hot to HOT wakeup event */
  640. /*trigger cold ,normal and hot interrupt */
  641. /* remove for temp THERMAL_WRAP_WR32(temp | 0xE0000000, TEMPMONINT);
  642. * enable trigger SPM interrupt */
  643. /*Only trigger hot interrupt */
  644. if (temperature2 > -275000)
  645. THERMAL_WRAP_WR32(temp | 0xC0000000, TEMPMONINT); /* enable trigger middle & Hot SPM interrupt */
  646. else
  647. THERMAL_WRAP_WR32(temp | 0x80000000, TEMPMONINT); /* enable trigger Hot SPM interrupt */
  648. }
  649. static int read_tc_raw_and_temp(volatile u32 *tempmsr_name, thermal_sensor_name ts_name,
  650. int *ts_raw)
  651. {
  652. int temp = 0, raw = 0;
  653. raw = (tempmsr_name != 0) ? (DRV_Reg32((tempmsr_name)) & 0x0fff) : 0;
  654. temp = (tempmsr_name != 0) ? raw_to_temperature_roomt(raw, ts_name) : 0;
  655. *ts_raw = raw;
  656. tscpu_dprintk("read_tc_raw_temp,ts_raw=%d,temp=%d\n", *ts_raw, temp * 100);
  657. return temp * 100;
  658. }
  659. void tscpu_thermal_read_bank_temp(thermal_bank_name bank, ts_e type, int order)
  660. {
  661. tscpu_dprintk("%s bank %d type %d order %d\n", __func__, bank, type, order);
  662. switch (order) {
  663. case 0:
  664. tscpu_bank_ts[bank][type] =
  665. read_tc_raw_and_temp((volatile u32 *)TEMPMSR0, type,
  666. &tscpu_bank_ts_r[bank][type]);
  667. tscpu_dprintk("%s order %d bank %d type %d tscpu_bank_ts %d tscpu_bank_ts_r %d\n",
  668. __func__, order, bank, type, tscpu_bank_ts[bank][type],
  669. tscpu_bank_ts_r[bank][type]);
  670. break;
  671. case 1:
  672. tscpu_bank_ts[bank][type] =
  673. read_tc_raw_and_temp((volatile u32 *)TEMPMSR1, type,
  674. &tscpu_bank_ts_r[bank][type]);
  675. tscpu_dprintk("%s order %d bank %d type %d tscpu_bank_ts %d tscpu_bank_ts_r %d\n",
  676. __func__, order, bank, type, tscpu_bank_ts[bank][type],
  677. tscpu_bank_ts_r[bank][type]);
  678. break;
  679. case 2:
  680. tscpu_bank_ts[bank][type] =
  681. read_tc_raw_and_temp((volatile u32 *)TEMPMSR2, type,
  682. &tscpu_bank_ts_r[bank][type]);
  683. tscpu_dprintk("%s order %d bank %d type %d tscpu_bank_ts %d tscpu_bank_ts_r %d\n",
  684. __func__, order, bank, type, tscpu_bank_ts[bank][type],
  685. tscpu_bank_ts_r[bank][type]);
  686. break;
  687. case 3:
  688. tscpu_bank_ts[bank][type] =
  689. read_tc_raw_and_temp((volatile u32 *)TEMPMSR3, type,
  690. &tscpu_bank_ts_r[bank][type]);
  691. tscpu_dprintk("%s order %d bank %d type %d tscpu_bank_ts %d tscpu_bank_ts_r %d\n",
  692. __func__, order, bank, type, tscpu_bank_ts[bank][type],
  693. tscpu_bank_ts_r[bank][type]);
  694. break;
  695. default:
  696. tscpu_bank_ts[bank][type] =
  697. read_tc_raw_and_temp((volatile u32 *)TEMPMSR0, type,
  698. &tscpu_bank_ts_r[bank][type]);
  699. tscpu_dprintk("%s order %d bank %d type %d tscpu_bank_ts %d tscpu_bank_ts_r %d\n",
  700. __func__, order, bank, type, tscpu_bank_ts[bank][type],
  701. tscpu_bank_ts_r[bank][type]);
  702. break;
  703. }
  704. }
  705. int tscpu_thermal_fast_init(void)
  706. {
  707. UINT32 temp = 0;
  708. UINT32 cunt = 0;
  709. /* UINT32 temp1 = 0,temp2 = 0,temp3 = 0,count=0; */
  710. /* tscpu_printk( "tscpu_thermal_fast_init\n"); */
  711. temp = THERMAL_INIT_VALUE;
  712. DRV_WriteReg32(PTPSPARE2, (0x00001000 + temp)); /* write temp to spare register */
  713. DRV_WriteReg32(TEMPMONCTL1, 1); /* counting unit is 320 * 31.25us = 10ms */
  714. DRV_WriteReg32(TEMPMONCTL2, 1); /* sensing interval is 200 * 10ms = 2000ms */
  715. DRV_WriteReg32(TEMPAHBPOLL, 1); /* polling interval to check if temperature sense is ready */
  716. DRV_WriteReg32(TEMPAHBTO, 0x000000FF); /* exceed this polling time, IRQ would be inserted */
  717. DRV_WriteReg32(TEMPMONIDET0, 0x00000000); /* times for interrupt occurrance */
  718. DRV_WriteReg32(TEMPMONIDET1, 0x00000000); /* times for interrupt occurrance */
  719. DRV_WriteReg32(TEMPMSRCTL0, 0x0000000); /* temperature measurement sampling control */
  720. /* this value will be stored to TEMPPNPMUXADDR (TEMPSPARE0) automatically by hw */
  721. DRV_WriteReg32(TEMPADCPNP0, 0x1);
  722. DRV_WriteReg32(TEMPADCPNP1, 0x2);
  723. DRV_WriteReg32(TEMPADCPNP2, 0x3);
  724. DRV_WriteReg32(TEMPADCPNP3, 0x4);
  725. #if 0
  726. DRV_WriteReg32(TEMPPNPMUXADDR, 0x1100B420); /* AHB address for pnp sensor mux selection */
  727. DRV_WriteReg32(TEMPADCMUXADDR, 0x1100B420); /* AHB address for auxadc mux selection */
  728. DRV_WriteReg32(TEMPADCENADDR, 0x1100B424); /* AHB address for auxadc enable */
  729. DRV_WriteReg32(TEMPADCVALIDADDR, 0x1100B428); /* AHB address for auxadc valid bit */
  730. DRV_WriteReg32(TEMPADCVOLTADDR, 0x1100B428); /* AHB address for auxadc voltage output */
  731. #else
  732. DRV_WriteReg32(TEMPPNPMUXADDR, (UINT32) PTPSPARE0_P); /* AHB address for pnp sensor mux selection */
  733. DRV_WriteReg32(TEMPADCMUXADDR, (UINT32) PTPSPARE0_P); /* AHB address for auxadc mux selection */
  734. DRV_WriteReg32(TEMPADCENADDR, (UINT32) PTPSPARE1_P); /* AHB address for auxadc enable */
  735. DRV_WriteReg32(TEMPADCVALIDADDR, (UINT32) PTPSPARE2_P); /* AHB address for auxadc valid bit */
  736. DRV_WriteReg32(TEMPADCVOLTADDR, (UINT32) PTPSPARE2_P); /* AHB address for auxadc voltage output */
  737. #endif
  738. DRV_WriteReg32(TEMPRDCTRL, 0x0); /* read valid & voltage are at the same register */
  739. /* indicate where the valid bit is (the 12th bit is valid bit and 1 is valid) */
  740. DRV_WriteReg32(TEMPADCVALIDMASK, 0x0000002C);
  741. DRV_WriteReg32(TEMPADCVOLTAGESHIFT, 0x0); /* do not need to shift */
  742. DRV_WriteReg32(TEMPADCWRITECTRL, 0x3); /* enable auxadc mux & pnp write transaction */
  743. /* enable all interrupt except filter sense and immediate sense interrupt */
  744. DRV_WriteReg32(TEMPMONINT, 0x00000000);
  745. DRV_WriteReg32(TEMPMONCTL0, 0x0000000F); /* enable all sensing point (sensing point 2 is unused) */
  746. cunt = 0;
  747. temp = DRV_Reg32(TEMPMSR0) & 0x0fff;
  748. while (temp != THERMAL_INIT_VALUE && cunt < 20) {
  749. cunt++;
  750. /* pr_debug("[Power/CPU_Thermal]0 temp=%d,cunt=%d\n",temp,cunt); */
  751. temp = DRV_Reg32(TEMPMSR0) & 0x0fff;
  752. }
  753. cunt = 0;
  754. temp = DRV_Reg32(TEMPMSR1) & 0x0fff;
  755. while (temp != THERMAL_INIT_VALUE && cunt < 20) {
  756. cunt++;
  757. /* pr_debug("[Power/CPU_Thermal]1 temp=%d,cunt=%d\n",temp,cunt); */
  758. temp = DRV_Reg32(TEMPMSR1) & 0x0fff;
  759. }
  760. cunt = 0;
  761. temp = DRV_Reg32(TEMPMSR2) & 0x0fff;
  762. while (temp != THERMAL_INIT_VALUE && cunt < 20) {
  763. cunt++;
  764. /* pr_debug("[Power/CPU_Thermal]2 temp=%d,cunt=%d\n",temp,cunt); */
  765. temp = DRV_Reg32(TEMPMSR2) & 0x0fff;
  766. }
  767. cunt = 0;
  768. temp = DRV_Reg32(TEMPMSR3) & 0x0fff;
  769. while (temp != THERMAL_INIT_VALUE && cunt < 20) {
  770. cunt++;
  771. /* pr_debug("[Power/CPU_Thermal]3 temp=%d,cunt=%d\n",temp,cunt); */
  772. temp = DRV_Reg32(TEMPMSR3) & 0x0fff;
  773. }
  774. return 0;
  775. }
  776. int tscpu_switch_bank(thermal_bank_name bank)
  777. {
  778. /* tscpu_dprintk( "tscpu_switch_bank =bank=%d\n",bank); */
  779. switch (bank) {
  780. case THERMAL_BANK0: /* CPU (TSMCU2) */
  781. thermal_clrl(PTPCORESEL, 0xF); /* bank0 */
  782. break;
  783. case THERMAL_BANK1: /* GPU (TSMCU1) */
  784. thermal_clrl(PTPCORESEL, 0xF);
  785. thermal_setl(PTPCORESEL, 0x1); /* bank1 */
  786. break;
  787. case THERMAL_BANK2: /* LTE (TSMCU3) */
  788. thermal_clrl(PTPCORESEL, 0xF);
  789. thermal_setl(PTPCORESEL, 0x2); /* bank2 */
  790. break;
  791. default:
  792. thermal_clrl(PTPCORESEL, 0xF); /* bank0 */
  793. break;
  794. }
  795. return 0;
  796. }
  797. void tscpu_thermal_initial_all_bank(void)
  798. {
  799. unsigned long flags;
  800. mt_ptp_lock(&flags);
  801. thermal_config_Bank();
  802. mt_ptp_unlock(&flags);
  803. }
  804. void tscpu_config_all_tc_hw_protect(int temperature, int temperature2)
  805. {
  806. int i = 0;
  807. int wd_api_ret;
  808. unsigned long flags;
  809. struct wd_api *wd_api;
  810. tscpu_dprintk("tscpu_config_all_tc_hw_protect,temperature=%d,temperature2=%d,\n",
  811. temperature, temperature2);
  812. #if THERMAL_PERFORMANCE_PROFILE
  813. struct timeval begin, end;
  814. unsigned long val;
  815. do_gettimeofday(&begin);
  816. #endif
  817. /*spend 860~1463 us */
  818. /*Thermal need to config to direct reset mode
  819. this API provide by Weiqi Fu(RGU SW owner). */
  820. wd_api_ret = get_wd_api(&wd_api);
  821. if (wd_api_ret >= 0) {
  822. wd_api->wd_thermal_direct_mode_config(WD_REQ_DIS, WD_REQ_RST_MODE); /* reset mode */
  823. } else {
  824. tscpu_printk("%d FAILED TO GET WD API\n", __LINE__);
  825. BUG();
  826. }
  827. #if THERMAL_PERFORMANCE_PROFILE
  828. do_gettimeofday(&end);
  829. /* Get milliseconds */
  830. pr_debug("resume time spent, sec : %lu , usec : %lu\n", (end.tv_sec - begin.tv_sec),
  831. (end.tv_usec - begin.tv_usec));
  832. #endif
  833. mt_ptp_lock(&flags);
  834. for (i = 0; i < TS_LEN_ARRAY(tscpu_g_bank); i++) {
  835. tscpu_switch_bank(i);
  836. set_tc_trigger_hw_protect(temperature, temperature2); /* Move thermal HW protection ahead... */
  837. }
  838. mt_ptp_unlock(&flags);
  839. /*Thermal need to config to direct reset mode
  840. this API provide by Weiqi Fu(RGU SW owner). */
  841. wd_api->wd_thermal_direct_mode_config(WD_REQ_EN, WD_REQ_RST_MODE); /* reset mode */
  842. }
  843. void tscpu_reset_thermal(void)
  844. {
  845. int temp = 0;
  846. /* reset thremal ctrl */
  847. temp = DRV_Reg32(INFRA_GLOBALCON_RST_0_SET);
  848. temp |= 0x00000001; /* 1: Enables thermal control software reset */
  849. THERMAL_WRAP_WR32(temp, INFRA_GLOBALCON_RST_0_SET);
  850. /* un reset */
  851. temp = DRV_Reg32(INFRA_GLOBALCON_RST_0_CLR);
  852. temp |= 0x00000001; /* 1: Enable reset Disables thermal control software reset */
  853. THERMAL_WRAP_WR32(temp, INFRA_GLOBALCON_RST_0_CLR);
  854. }
  855. int tscpu_read_temperature_info(struct seq_file *m, void *v)
  856. {
  857. seq_printf(m, "current temp:%d\n", tscpu_read_curr_temp);
  858. seq_printf(m, "calefuse1:0x%x\n", calefuse1);
  859. seq_printf(m, "calefuse2:0x%x\n", calefuse2);
  860. seq_printf(m, "calefuse3:0x%x\n", calefuse3);
  861. seq_printf(m, "g_adc_ge_t:%d\n", g_adc_ge_t);
  862. seq_printf(m, "g_adc_oe_t:%d\n", g_adc_oe_t);
  863. seq_printf(m, "g_degc_cali:%d\n", g_degc_cali);
  864. seq_printf(m, "g_adc_cali_en_t:%d\n", g_adc_cali_en_t);
  865. seq_printf(m, "g_o_slope:%d\n", g_o_slope);
  866. seq_printf(m, "g_o_slope_sign:%d\n", g_o_slope_sign);
  867. seq_printf(m, "g_id:%d\n", g_id);
  868. seq_printf(m, "g_o_vtsmcu1:%d\n", g_o_vtsmcu1);
  869. seq_printf(m, "g_o_vtsmcu2:%d\n", g_o_vtsmcu2);
  870. seq_printf(m, "g_o_vtsabb:%d\n", g_o_vtsabb);
  871. return 0;
  872. }
  873. int tscpu_get_curr_temp(void)
  874. {
  875. tscpu_update_tempinfo();
  876. return tscpu_max_temperature();
  877. }
  878. #ifdef CONFIG_OF
  879. int get_io_reg_base(void)
  880. {
  881. struct device_node *node = NULL;
  882. node = of_find_compatible_node(NULL, NULL, "mediatek,THERM_CTRL");
  883. BUG_ON(node == 0);
  884. if (node) {
  885. /* Setup IO addresses */
  886. thermal_base = of_iomap(node, 0);
  887. /* pr_debug("[THERM_CTRL] thermal_base=0x%p\n",thermal_base); */
  888. }
  889. /*get thermal irq num */
  890. thermal_irq_number = irq_of_parse_and_map(node, 0);
  891. pr_debug("[THERM_CTRL] thermal_irq_number=%d\n", thermal_irq_number);
  892. if (!thermal_irq_number) {
  893. pr_debug("[THERM_CTRL] get irqnr failed=%d\n", thermal_irq_number);
  894. return 0;
  895. }
  896. of_property_read_u32(node, "reg", &thermal_phy_base);
  897. /* pr_debug("[THERM_CTRL] thermal_base thermal_phy_base=0x%x\n",thermal_phy_base); */
  898. node = of_find_compatible_node(NULL, NULL, "mediatek,mt6735-auxadc");
  899. BUG_ON(node == 0);
  900. if (node) {
  901. /* Setup IO addresses */
  902. auxadc_ts_base = of_iomap(node, 0);
  903. /* pr_debug("[THERM_CTRL] auxadc_ts_base=0x%p\n",auxadc_ts_base); */
  904. }
  905. of_property_read_u32(node, "reg", &auxadc_ts_phy_base);
  906. /* pr_debug("[THERM_CTRL] auxadc_ts_phy_base=0x%x\n",auxadc_ts_phy_base); */
  907. node = of_find_compatible_node(NULL, NULL, "mediatek,INFRACFG_AO");
  908. BUG_ON(node == 0);
  909. if (node) {
  910. /* Setup IO addresses */
  911. infracfg_ao_base = of_iomap(node, 0);
  912. /* pr_debug("[THERM_CTRL] infracfg_ao_base=0x%p\n",infracfg_ao_base); */
  913. }
  914. node = of_find_compatible_node(NULL, NULL, "mediatek,APMIXED");
  915. BUG_ON(node == 0);
  916. if (node) {
  917. /* Setup IO addresses */
  918. apmixed_base = of_iomap(node, 0);
  919. /* pr_debug("[THERM_CTRL] apmixed_base=0x%p\n",apmixed_base); */
  920. }
  921. of_property_read_u32(node, "reg", &apmixed_phy_base);
  922. /* pr_debug("[THERM_CTRL] apmixed_phy_base=0x%x\n",apmixed_phy_base); */
  923. return 1;
  924. }
  925. #endif