mtk_ts_cpu.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417
  1. /*
  2. * Copyright (C) 2015 MediaTek Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define DEBUG 1
  14. #include <linux/version.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/dmi.h>
  18. #include <linux/acpi.h>
  19. #include <linux/thermal.h>
  20. #include <linux/platform_device.h>
  21. #include <mt-plat/aee.h>
  22. #include <linux/types.h>
  23. #include <linux/delay.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/spinlock.h>
  26. #include <mt-plat/sync_write.h>
  27. #include "mt-plat/mtk_thermal_monitor.h"
  28. #include <linux/seq_file.h>
  29. #include <linux/slab.h>
  30. #include "mtk_thermal_typedefs.h"
  31. #include "mach/mt_thermal.h"
  32. #if defined(CONFIG_MTK_CLKMGR)
  33. #include <mach/mt_clkmgr.h>
  34. #else
  35. #include <linux/clk.h>
  36. #endif
  37. #include <mt_ptp.h>
  38. /* #include <mach/mt_wtd.h> */
  39. #include <mach/wd_api.h>
  40. #include <mtk_gpu_utility.h>
  41. #include <linux/time.h>
  42. #include <tscpu_settings.h>
  43. #ifdef CONFIG_OF
  44. #include <linux/of.h>
  45. #include <linux/of_irq.h>
  46. #include <linux/of_address.h>
  47. #endif
  48. #define __MT_MTK_TS_CPU_C__
  49. #if MTK_TS_CPU_RT
  50. #include <linux/sched.h>
  51. #include <linux/kthread.h>
  52. #endif
  53. #if defined(CONFIG_ARCH_MT6755)
  54. #include "mach/mt_ppm_api.h"
  55. #else
  56. #include "mt_cpufreq.h"
  57. #endif
  58. #include <linux/uidgid.h>
  59. #include "mt_auxadc.h"
  60. /*=============================================================
  61. *Local variable definition
  62. *=============================================================*/
  63. static kuid_t uid = KUIDT_INIT(0);
  64. static kgid_t gid = KGIDT_INIT(1000);
  65. #if !defined(CONFIG_MTK_CLKMGR)
  66. struct clk *therm_main; /* main clock for Thermal */
  67. #if defined(CONFIG_ARCH_MT6755)
  68. /*Patch to pause thermal controller and turn off auxadc GC.
  69. For mt6755 only*/
  70. struct clk *therm_auxadc;
  71. #endif
  72. #endif
  73. void __iomem *therm_clk_infracfg_ao_base;
  74. #ifdef CONFIG_OF
  75. u32 thermal_irq_number = 0;
  76. void __iomem *thermal_base;
  77. void __iomem *auxadc_ts_base;
  78. void __iomem *infracfg_ao_base;
  79. #if defined(CONFIG_ARCH_MT6755)
  80. void __iomem *th_apmixed_base;
  81. #else
  82. void __iomem *apmixed_base;
  83. #endif
  84. void __iomem *INFRACFG_AO_base;
  85. int thermal_phy_base;
  86. int auxadc_ts_phy_base;
  87. int apmixed_phy_base;
  88. int pericfg_phy_base;
  89. #endif
  90. static unsigned int interval = 1000; /* mseconds, 0 : no auto polling */
  91. int tscpu_g_curr_temp = 0;
  92. int tscpu_g_prev_temp = 0;
  93. static int g_max_temp = 50000; /* default=50 deg */
  94. #if defined(CONFIG_ARCH_MT6753)
  95. /*For MT6753 PMIC 5A throttle patch*/
  96. static int thermal5A_TH = 55000;
  97. static int thermal5A_status;
  98. #endif
  99. static int tc_mid_trip = -275000;
  100. /* trip_temp[0] must be initialized to the thermal HW protection point. */
  101. #if !defined(CONFIG_ARCH_MT6755)
  102. static int trip_temp[10] = { 117000, 100000, 85000, 75000, 65000, 55000, 45000, 35000, 25000, 15000 };
  103. #else
  104. static int trip_temp[10] = { 117000, 90000, 85000, 75000, 65000, 55000, 45000, 35000, 25000, 15000 };
  105. #endif
  106. int tscpu_read_curr_temp;
  107. static bool talking_flag;
  108. static int kernelmode;
  109. static int g_THERMAL_TRIP[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  110. static int temperature_switch;
  111. static int num_trip = 5;
  112. static int tscpu_num_opp;
  113. static struct mtk_cpu_power_info *mtk_cpu_power;
  114. static int g_tc_resume; /* default=0,read temp */
  115. static int MA_len_temp;
  116. static int proc_write_flag;
  117. static struct thermal_zone_device *thz_dev;
  118. static char g_bind0[20] = "mtktscpu-sysrst";
  119. #if !defined(CONFIG_ARCH_MT6755)
  120. static char g_bind1[20] = "cpu02";
  121. static char g_bind2[20] = "cpu15";
  122. static char g_bind3[20] = "cpu22";
  123. static char g_bind4[20] = "cpu28";
  124. #else
  125. static char g_bind1[20] = "cpu00";
  126. static char g_bind2[20] = "cpu00";
  127. static char g_bind3[20] = "cpu03";
  128. static char g_bind4[20] = "cpu04";
  129. #endif
  130. static char g_bind5[20] = "";
  131. static char g_bind6[20] = "";
  132. static char g_bind7[20] = "";
  133. static char g_bind8[20] = "";
  134. static char g_bind9[20] = "";
  135. struct mt_gpufreq_power_table_info *mtk_gpu_power = NULL;
  136. #if 0
  137. int Num_of_GPU_OPP = 1; /* Set this value =1 for non-DVS GPU */
  138. #else /* DVFS GPU */
  139. int Num_of_GPU_OPP = 0;
  140. #endif
  141. /*=============================================================
  142. * Local function definition
  143. *=============================================================*/
  144. #if (CONFIG_THERMAL_AEE_RR_REC == 1)
  145. static void _mt_thermal_aee_init(void)
  146. {
  147. aee_rr_rec_thermal_temp1(0xFF);
  148. aee_rr_rec_thermal_temp2(0xFF);
  149. aee_rr_rec_thermal_temp3(0xFF);
  150. aee_rr_rec_thermal_temp4(0xFF);
  151. aee_rr_rec_thermal_temp5(0xFF);
  152. aee_rr_rec_thermal_status(0xFF);
  153. }
  154. #endif
  155. static int tscpu_thermal_probe(struct platform_device *dev);
  156. static int tscpu_register_thermal(void);
  157. static void tscpu_unregister_thermal(void);
  158. #if THERMAL_DRV_UPDATE_TEMP_DIRECT_TO_MET
  159. static int a_tscpu_all_temp[MTK_THERMAL_SENSOR_CPU_COUNT] = { 0 };
  160. static DEFINE_MUTEX(MET_GET_TEMP_LOCK);
  161. static met_thermalsampler_funcMET g_pThermalSampler;
  162. void mt_thermalsampler_registerCB(met_thermalsampler_funcMET pCB)
  163. {
  164. g_pThermalSampler = pCB;
  165. }
  166. EXPORT_SYMBOL(mt_thermalsampler_registerCB);
  167. static DEFINE_SPINLOCK(tscpu_met_spinlock);
  168. void tscpu_met_lock(unsigned long *flags)
  169. {
  170. spin_lock_irqsave(&tscpu_met_spinlock, *flags);
  171. }
  172. EXPORT_SYMBOL(tscpu_met_lock);
  173. void tscpu_met_unlock(unsigned long *flags)
  174. {
  175. spin_unlock_irqrestore(&tscpu_met_spinlock, *flags);
  176. }
  177. EXPORT_SYMBOL(tscpu_met_unlock);
  178. #endif
  179. static int g_is_temp_valid;
  180. static void temp_valid_lock(unsigned long *flags);
  181. static void temp_valid_unlock(unsigned long *flags);
  182. /*=============================================================
  183. *Weak functions
  184. *=============================================================*/
  185. int __attribute__ ((weak))
  186. IMM_IsAdcInitReady(void)
  187. {
  188. pr_err("E_WF: %s doesn't exist\n", __func__);
  189. return 0;
  190. }
  191. #if defined(CONFIG_ARCH_MT6755)
  192. void __attribute__ ((weak))
  193. mt_ppm_cpu_thermal_protect(unsigned int limited_power)
  194. {
  195. pr_err("E_WF: %s doesn't exist\n", __func__);
  196. }
  197. #else
  198. void __attribute__ ((weak))
  199. mt_cpufreq_thermal_protect(unsigned int limited_power)
  200. {
  201. pr_err("E_WF: %s doesn't exist\n", __func__);
  202. }
  203. #endif
  204. bool __attribute__ ((weak))
  205. mtk_get_gpu_loading(unsigned int *pLoading)
  206. {
  207. pr_err("E_WF: %s doesn't exist\n", __func__);
  208. return 0;
  209. }
  210. void __attribute__ ((weak))
  211. mt_ptp_lock(unsigned long *flags)
  212. {
  213. pr_err("E_WF: %s doesn't exist\n", __func__);
  214. }
  215. void __attribute__ ((weak))
  216. mt_ptp_unlock(unsigned long *flags)
  217. {
  218. pr_err("E_WF: %s doesn't exist\n", __func__);
  219. }
  220. int __attribute__ ((weak))
  221. get_immediate_ts1_wrap(void)
  222. {
  223. return 0;
  224. }
  225. int __attribute__ ((weak))
  226. get_immediate_ts2_wrap(void)
  227. {
  228. return 0;
  229. }
  230. int __attribute__ ((weak))
  231. get_immediate_ts3_wrap(void)
  232. {
  233. return 0;
  234. }
  235. int __attribute__ ((weak))
  236. get_immediate_ts4_wrap(void)
  237. {
  238. return 0;
  239. }
  240. int __attribute__ ((weak))
  241. get_immediate_tsabb_wrap(void)
  242. {
  243. return 0;
  244. }
  245. void __attribute__ ((weak))
  246. mt_cpufreq_thermal_5A_limit(bool enable)
  247. {
  248. pr_err("E_WF: %s doesn't exist\n", __func__);
  249. }
  250. /*=============================================================*/
  251. static void tscpu_fast_initial_sw_workaround(void)
  252. {
  253. int i = 0;
  254. unsigned long flags;
  255. /* tscpu_printk("tscpu_fast_initial_sw_workaround\n"); */
  256. /* tscpu_thermal_clock_on(); */
  257. mt_ptp_lock(&flags);
  258. for (i = 0; i < TS_LEN_ARRAY(tscpu_g_bank); i++) {
  259. tscpu_switch_bank(i);
  260. tscpu_thermal_fast_init();
  261. }
  262. mt_ptp_unlock(&flags);
  263. temp_valid_lock(&flags);
  264. g_is_temp_valid = 0;
  265. temp_valid_unlock(&flags);
  266. }
  267. void tscpu_thermal_tempADCPNP(int adc, int order)
  268. {
  269. tscpu_dprintk("%s adc %x, order %d\n", __func__, adc, order);
  270. switch (order) {
  271. case 0:
  272. THERMAL_WRAP_WR32(adc, TEMPADCPNP0);
  273. break;
  274. case 1:
  275. THERMAL_WRAP_WR32(adc, TEMPADCPNP1);
  276. break;
  277. case 2:
  278. THERMAL_WRAP_WR32(adc, TEMPADCPNP2);
  279. break;
  280. case 3:
  281. THERMAL_WRAP_WR32(adc, TEMPADCPNP3);
  282. break;
  283. default:
  284. THERMAL_WRAP_WR32(adc, TEMPADCPNP0);
  285. break;
  286. }
  287. }
  288. int tscpu_thermal_ADCValueOfMcu(enum thermal_sensor_enum type)
  289. {
  290. switch (type) {
  291. case MCU1:
  292. return TEMPADC_MCU1;
  293. case MCU2:
  294. return TEMPADC_MCU2;
  295. case MCU3:
  296. return TEMPADC_MCU3;
  297. case MCU4:
  298. return TEMPADC_MCU4;
  299. case ABB:
  300. return TEMPADC_ABB;
  301. default:
  302. return TEMPADC_MCU1;
  303. }
  304. }
  305. static int max_temperature_in_bank(thermal_bank_name bank)
  306. {
  307. int j = 0;
  308. int max_in_bank = 0;
  309. for (j = 0; j < tscpu_g_bank[bank].ts_number; j++) {
  310. if (tscpu_bank_ts[bank][tscpu_g_bank[bank].ts[j].type] > max_in_bank)
  311. max_in_bank = tscpu_bank_ts[bank][tscpu_g_bank[bank].ts[j].type];
  312. tscpu_dprintk("tscpu_get_temp CPU bank%d T%d=%d\n", bank, j,
  313. tscpu_bank_ts[bank][tscpu_g_bank[bank].ts[j].type]);
  314. }
  315. return max_in_bank;
  316. }
  317. int tscpu_max_temperature(void)
  318. {
  319. int i = 0;
  320. int max = 0;
  321. int max_in_bank = 0;
  322. tscpu_dprintk("tscpu_get_temp %s, %d\n", __func__, __LINE__);
  323. for (i = 0; i < TS_LEN_ARRAY(tscpu_g_bank); i++) {
  324. max_in_bank = max_temperature_in_bank(i);
  325. if (max_in_bank > max)
  326. max = max_in_bank;
  327. }
  328. return max;
  329. }
  330. void tscpu_print_all_temperature(int isDprint)
  331. {
  332. int i = 0, j = 0;
  333. for (i = 0; i < TS_LEN_ARRAY(tscpu_g_bank); i++) {
  334. for (j = 0; j < tscpu_g_bank[i].ts_number; j++) {
  335. if (isDprint)
  336. tscpu_dprintk("%d ", tscpu_bank_ts[i][tscpu_g_bank[i].ts[j].type]);
  337. else
  338. tscpu_printk("%d ", tscpu_bank_ts[i][tscpu_g_bank[i].ts[j].type]);
  339. }
  340. }
  341. if (isDprint)
  342. tscpu_dprintk("\n");
  343. else
  344. tscpu_printk("\n");
  345. }
  346. void set_taklking_flag(bool flag)
  347. {
  348. talking_flag = flag;
  349. tscpu_printk("talking_flag=%d\n", talking_flag);
  350. }
  351. int mtk_gpufreq_register(struct mt_gpufreq_power_table_info *freqs, int num)
  352. {
  353. int i = 0;
  354. tscpu_dprintk("mtk_gpufreq_register\n");
  355. mtk_gpu_power = kzalloc((num) * sizeof(struct mt_gpufreq_power_table_info), GFP_KERNEL);
  356. if (mtk_gpu_power == NULL)
  357. return -ENOMEM;
  358. for (i = 0; i < num; i++) {
  359. mtk_gpu_power[i].gpufreq_khz = freqs[i].gpufreq_khz;
  360. mtk_gpu_power[i].gpufreq_power = freqs[i].gpufreq_power;
  361. tscpu_dprintk("[%d].gpufreq_khz=%u, .gpufreq_power=%u\n",
  362. i, freqs[i].gpufreq_khz, freqs[i].gpufreq_power);
  363. }
  364. Num_of_GPU_OPP = num; /* GPU OPP count */
  365. return 0;
  366. }
  367. EXPORT_SYMBOL(mtk_gpufreq_register);
  368. static int tscpu_bind(struct thermal_zone_device *thermal, struct thermal_cooling_device *cdev)
  369. {
  370. int table_val = 0;
  371. if (!strcmp(cdev->type, g_bind0)) {
  372. table_val = 0;
  373. tscpu_config_all_tc_hw_protect(trip_temp[0], tc_mid_trip);
  374. /* tscpu_dprintk("tscpu_bind %s\n", cdev->type); */
  375. } else if (!strcmp(cdev->type, g_bind1)) {
  376. table_val = 1;
  377. /* only when a valid cooler is tried to bind here, we set tc_mid_trip to trip_temp[1]; */
  378. tc_mid_trip = trip_temp[1];
  379. tscpu_config_all_tc_hw_protect(trip_temp[0], tc_mid_trip);
  380. /* tscpu_dprintk("tscpu_bind %s\n", cdev->type); */
  381. } else if (!strcmp(cdev->type, g_bind2)) {
  382. table_val = 2;
  383. /* tscpu_dprintk("tscpu_bind %s\n", cdev->type); */
  384. } else if (!strcmp(cdev->type, g_bind3)) {
  385. table_val = 3;
  386. /* tscpu_dprintk("tscpu_bind %s\n", cdev->type); */
  387. } else if (!strcmp(cdev->type, g_bind4)) {
  388. table_val = 4;
  389. /* tscpu_dprintk("tscpu_bind %s\n", cdev->type); */
  390. } else if (!strcmp(cdev->type, g_bind5)) {
  391. table_val = 5;
  392. /* tscpu_dprintk("tscpu_bind %s\n", cdev->type); */
  393. } else if (!strcmp(cdev->type, g_bind6)) {
  394. table_val = 6;
  395. /* tscpu_dprintk("tscpu_bind %s\n", cdev->type); */
  396. } else if (!strcmp(cdev->type, g_bind7)) {
  397. table_val = 7;
  398. /* tscpu_dprintk("tscpu_bind %s\n", cdev->type); */
  399. } else if (!strcmp(cdev->type, g_bind8)) {
  400. table_val = 8;
  401. /* tscpu_dprintk("tscpu_bind %s\n", cdev->type); */
  402. } else if (!strcmp(cdev->type, g_bind9)) {
  403. table_val = 9;
  404. /* tscpu_dprintk("tscpu_bind %s\n", cdev->type); */
  405. } else {
  406. return 0;
  407. }
  408. if (mtk_thermal_zone_bind_cooling_device(thermal, table_val, cdev)) {
  409. tscpu_warn("tscpu_bind error binding cooling dev\n");
  410. return -EINVAL;
  411. }
  412. tscpu_printk("tscpu_bind binding OK, %d\n", table_val);
  413. return 0;
  414. }
  415. static int tscpu_unbind(struct thermal_zone_device *thermal, struct thermal_cooling_device *cdev)
  416. {
  417. int table_val = 0;
  418. if (!strcmp(cdev->type, g_bind0)) {
  419. table_val = 0;
  420. /* tscpu_dprintk("tscpu_unbind %s\n", cdev->type); */
  421. } else if (!strcmp(cdev->type, g_bind1)) {
  422. table_val = 1;
  423. /* only when a valid cooler is tried to bind here, we set tc_mid_trip to trip_temp[1]; */
  424. tc_mid_trip = -275000;
  425. /* tscpu_dprintk("tscpu_unbind %s\n", cdev->type); */
  426. } else if (!strcmp(cdev->type, g_bind2)) {
  427. table_val = 2;
  428. /* tscpu_dprintk("tscpu_unbind %s\n", cdev->type); */
  429. } else if (!strcmp(cdev->type, g_bind3)) {
  430. table_val = 3;
  431. /* tscpu_dprintk("tscpu_unbind %s\n", cdev->type); */
  432. } else if (!strcmp(cdev->type, g_bind4)) {
  433. table_val = 4;
  434. /* tscpu_dprintk("tscpu_unbind %s\n", cdev->type); */
  435. } else if (!strcmp(cdev->type, g_bind5)) {
  436. table_val = 5;
  437. /* tscpu_dprintk("tscpu_unbind %s\n", cdev->type); */
  438. } else if (!strcmp(cdev->type, g_bind6)) {
  439. table_val = 6;
  440. /* tscpu_dprintk("tscpu_unbind %s\n", cdev->type); */
  441. } else if (!strcmp(cdev->type, g_bind7)) {
  442. table_val = 7;
  443. /* tscpu_dprintk("tscpu_unbind %s\n", cdev->type); */
  444. } else if (!strcmp(cdev->type, g_bind8)) {
  445. table_val = 8;
  446. /* tscpu_dprintk("tscpu_unbind %s\n", cdev->type); */
  447. } else if (!strcmp(cdev->type, g_bind9)) {
  448. table_val = 9;
  449. /* tscpu_dprintk("tscpu_unbind %s\n", cdev->type); */
  450. } else
  451. return 0;
  452. if (thermal_zone_unbind_cooling_device(thermal, table_val, cdev)) {
  453. tscpu_warn("tscpu_unbind error unbinding cooling dev\n");
  454. return -EINVAL;
  455. }
  456. tscpu_printk("tscpu_unbind unbinding OK\n");
  457. return 0;
  458. }
  459. static int tscpu_get_mode(struct thermal_zone_device *thermal, enum thermal_device_mode *mode)
  460. {
  461. *mode = (kernelmode) ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED;
  462. return 0;
  463. }
  464. static int tscpu_set_mode(struct thermal_zone_device *thermal, enum thermal_device_mode mode)
  465. {
  466. kernelmode = mode;
  467. return 0;
  468. }
  469. static int tscpu_get_trip_type(struct thermal_zone_device *thermal, int trip,
  470. enum thermal_trip_type *type)
  471. {
  472. *type = g_THERMAL_TRIP[trip];
  473. return 0;
  474. }
  475. static int tscpu_get_trip_temp(struct thermal_zone_device *thermal, int trip, unsigned long *temp)
  476. {
  477. *temp = trip_temp[trip];
  478. return 0;
  479. }
  480. static int tscpu_get_crit_temp(struct thermal_zone_device *thermal, unsigned long *temperature)
  481. {
  482. *temperature = MTKTSCPU_TEMP_CRIT;
  483. return 0;
  484. }
  485. static int tscpu_get_temp(struct thermal_zone_device *thermal, unsigned long *t)
  486. {
  487. int ret = 0;
  488. int curr_temp;
  489. int temp_temp;
  490. static int last_cpu_real_temp;
  491. curr_temp = tscpu_get_curr_temp();
  492. tscpu_dprintk("tscpu_get_temp CPU Max T=%d\n", curr_temp);
  493. if ((curr_temp > (trip_temp[0] - 15000)) || (curr_temp < -30000) || (curr_temp > 85000)) {
  494. printk_ratelimited("%d %d CPU T=%d\n",
  495. get_adaptive_power_limit(0), get_adaptive_power_limit(1), curr_temp);
  496. }
  497. temp_temp = curr_temp;
  498. if (curr_temp != 0) {/* not resumed from suspensio... */
  499. if ((curr_temp > 150000) || (curr_temp < -20000)) { /* invalid range */
  500. tscpu_warn("CPU temp invalid=%d\n", curr_temp);
  501. temp_temp = 50000;
  502. ret = -1;
  503. } else if (last_cpu_real_temp != 0) {
  504. if ((curr_temp - last_cpu_real_temp > 40000) || (last_cpu_real_temp - curr_temp > 40000)) {
  505. /* delta 40C, invalid change */
  506. tscpu_warn("CPU temp float hugely temp=%d, lasttemp=%d\n",
  507. curr_temp, last_cpu_real_temp);
  508. /* tscpu_printk("RAW_TS2 = %d,RAW_TS3 = %d,RAW_TS4 = %d\n",RAW_TS2,RAW_TS3,RAW_TS4); */
  509. temp_temp = 50000;
  510. ret = -1;
  511. }
  512. }
  513. }
  514. last_cpu_real_temp = curr_temp;
  515. curr_temp = temp_temp;
  516. tscpu_read_curr_temp = curr_temp;
  517. *t = (unsigned long)curr_temp;
  518. #if MTKTSCPU_FAST_POLLING
  519. tscpu_cur_fp_factor = tscpu_next_fp_factor;
  520. if (curr_temp >= fast_polling_trip_temp) {
  521. tscpu_next_fp_factor = fast_polling_factor;
  522. /* it means next timeout will be in interval/fast_polling_factor */
  523. thermal->polling_delay = interval / fast_polling_factor;
  524. } else {
  525. tscpu_next_fp_factor = 1;
  526. thermal->polling_delay = interval;
  527. }
  528. #endif
  529. /* for low power */
  530. if ((int)*t >= tscpu_polling_trip_temp1)
  531. ;
  532. else if ((int)*t < tscpu_polling_trip_temp2)
  533. thermal->polling_delay = interval * tscpu_polling_factor2;
  534. else
  535. thermal->polling_delay = interval * tscpu_polling_factor1;
  536. /* tscpu_dprintk("tscpu_get_temp:thermal->polling_delay=%d\n",thermal->polling_delay); */
  537. #if CPT_ADAPTIVE_AP_COOLER
  538. tscpu_g_prev_temp = tscpu_g_curr_temp;
  539. tscpu_g_curr_temp = curr_temp;
  540. #endif
  541. #if THERMAL_GPIO_OUT_TOGGLE
  542. /*for output signal monitor */
  543. tscpu_set_GPIO_toggle_for_monitor();
  544. #endif
  545. #if defined(CONFIG_ARCH_MT6753)
  546. /*For MT6753 PMIC 5A throttle patch*/
  547. if (curr_temp >= thermal5A_TH && thermal5A_status == 0) {
  548. mt_cpufreq_thermal_5A_limit(1);
  549. thermal5A_status = 1;
  550. } else if (curr_temp < thermal5A_TH && thermal5A_status == 1) {
  551. mt_cpufreq_thermal_5A_limit(0);
  552. thermal5A_status = 0;
  553. }
  554. #endif
  555. g_max_temp = curr_temp;
  556. return ret;
  557. }
  558. /* bind callback functions to thermalzone */
  559. static struct thermal_zone_device_ops mtktscpu_dev_ops = {
  560. .bind = tscpu_bind,
  561. .unbind = tscpu_unbind,
  562. .get_temp = tscpu_get_temp,
  563. .get_mode = tscpu_get_mode,
  564. .set_mode = tscpu_set_mode,
  565. .get_trip_type = tscpu_get_trip_type,
  566. .get_trip_temp = tscpu_get_trip_temp,
  567. .get_crit_temp = tscpu_get_crit_temp,
  568. };
  569. static int tscpu_read_Tj_out(struct seq_file *m, void *v)
  570. {
  571. int ts_con0 = 0;
  572. /* TS_CON0[19:16] = 0x8: Tj sensor Analog signal output via HW pin */
  573. ts_con0 = DRV_Reg32(TS_CON0_TM);
  574. seq_printf(m, "TS_CON0:0x%x\n", ts_con0);
  575. return 0;
  576. }
  577. static ssize_t tscpu_write_Tj_out(struct file *file, const char __user *buffer, size_t count,
  578. loff_t *data)
  579. {
  580. char desc[32];
  581. int lv_Tj_out_flag;
  582. int len = 0;
  583. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  584. if (copy_from_user(desc, buffer, len))
  585. return 0;
  586. desc[len] = '\0';
  587. if (kstrtoint(desc, 10, &lv_Tj_out_flag) == 0) {
  588. if (lv_Tj_out_flag == 1) {
  589. /* TS_CON0[19:16] = 0x8: Tj sensor Analog signal output via HW pin */
  590. THERMAL_WRAP_WR32(DRV_Reg32(TS_CON0_TM) | 0x00010000, TS_CON0_TM);
  591. } else {
  592. /* TS_CON0[19:16] = 0x8: Tj sensor Analog signal output via HW pin */
  593. THERMAL_WRAP_WR32(DRV_Reg32(TS_CON0_TM) & 0xfffeffff, TS_CON0_TM);
  594. }
  595. tscpu_dprintk("tscpu_write_Tj_out lv_Tj_out_flag=%d\n", lv_Tj_out_flag);
  596. return count;
  597. }
  598. tscpu_dprintk("tscpu_write_Tj_out bad argument\n");
  599. return -EINVAL;
  600. }
  601. #if THERMAL_GPIO_OUT_TOGGLE
  602. static int g_trigger_temp = 95000; /* default 95 deg */
  603. static int g_GPIO_out_enable; /* 0:disable */
  604. static int g_GPIO_already_set;
  605. #define GPIO118_MODE (GPIO_BASE + 0x0770)
  606. #define GPIO118_DIR (GPIO_BASE + 0x0070)
  607. #define GPIO118_DOUT (GPIO_BASE + 0x0470)
  608. void tscpu_set_GPIO_toggle_for_monitor(void)
  609. {
  610. int lv_GPIO118_MODE, lv_GPIO118_DIR, lv_GPIO118_DOUT;
  611. tscpu_dprintk("tscpu_set_GPIO_toggle_for_monitor,g_GPIO_out_enable=%d\n",
  612. g_GPIO_out_enable);
  613. if (g_GPIO_out_enable == 1) {
  614. if (g_max_temp > g_trigger_temp) {
  615. tscpu_printk("g_max_temp %d > g_trigger_temp %d\n", g_max_temp,
  616. g_trigger_temp);
  617. g_GPIO_out_enable = 0; /* only can enter once */
  618. g_GPIO_already_set = 1;
  619. lv_GPIO118_MODE = thermal_readl(GPIO118_MODE);
  620. lv_GPIO118_DIR = thermal_readl(GPIO118_DIR);
  621. lv_GPIO118_DOUT = thermal_readl(GPIO118_DOUT);
  622. tscpu_printk("tscpu_set_GPIO_toggle_for_monitor:lv_GPIO118_MODE=0x%x,", lv_GPIO118_MODE);
  623. tscpu_printk("lv_GPIO118_DIR=0x%x,lv_GPIO118_DOUT=0x%x,\n", lv_GPIO118_DIR, lv_GPIO118_DOUT);
  624. /* thermal_clrl(GPIO118_MODE,0x00000E00);//clear GPIO118_MODE[11:9] */
  625. /* thermal_setl(GPIO118_DIR, 0x00000040);//set GPIO118_DIR[6]=1 */
  626. thermal_clrl(GPIO118_DOUT, 0x00000040); /* set GPIO118_DOUT[6]=0 Low */
  627. udelay(200);
  628. thermal_setl(GPIO118_DOUT, 0x00000040); /* set GPIO118_DOUT[6]=1 Hiht */
  629. } else {
  630. if (g_GPIO_already_set == 1) {
  631. /* restore */
  632. g_GPIO_already_set = 0;
  633. /* thermal_writel(GPIO118_MODE,lv_GPIO118_MODE); */
  634. /* thermal_writel(GPIO118_DIR, lv_GPIO118_DIR); */
  635. /* thermal_writel(GPIO118_DOUT,lv_GPIO118_DOUT); */
  636. thermal_clrl(GPIO118_DOUT, 0x00000040); /* set GPIO118_DOUT[6]=0 Low */
  637. }
  638. }
  639. }
  640. }
  641. /*
  642. For example:
  643. GPIO_BASE :0x10005000
  644. GPIO118_MODE = 0 (change to GPIO mode)
  645. 0x0770 GPIO_MODE24 16 GPIO Mode Control Register 24
  646. 11 9 GPIO118_MODE RW Public 3'd1 "0: GPIO118 (IO)1: UTXD3 (O)2: URXD3 (I)3: MD_UTXD (O)
  647. 4: LTE_UTXD (O)5: TDD_TXD (O)6: Reserved7: DBG_MON_A_10_ (IO)" Selects GPIO 118 mode
  648. GPIO118_DIR =1 (output)
  649. 0x0070 GPIO_DIR8 16 GPIO Direction Control Register 8
  650. 6 6 GPIO118_DIR RW Public 1'b0 "0: Input1: Output" GPIO 118 direction control
  651. GPIO118_DOUT=1/0 (hi or low)
  652. 0x0470 GPIO_DOUT8 16 GPIO Data Output Register 8
  653. 6 6 GPIO118_DOUT RW Public 1'b0 "0: Output 01: Output 1" GPIO 118 data output value
  654. */
  655. static int tscpu_read_GPIO_out(struct seq_file *m, void *v)
  656. {
  657. seq_printf(m, "GPIO out enable:%d, trigger temperature=%d\n", g_GPIO_out_enable,
  658. g_trigger_temp);
  659. return 0;
  660. }
  661. static ssize_t tscpu_write_GPIO_out(struct file *file, const char __user *buffer, size_t count,
  662. loff_t *data)
  663. {
  664. char desc[512];
  665. char TEMP[10], ENABLE[10];
  666. unsigned int valTEMP, valENABLE;
  667. int len = 0;
  668. int lv_GPIO118_MODE, lv_GPIO118_DIR;
  669. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  670. if (copy_from_user(desc, buffer, len))
  671. return 0;
  672. desc[len] = '\0';
  673. if (sscanf(desc, "%9s %d %9s %d ", TEMP, &valTEMP, ENABLE, &valENABLE) == 4) {
  674. /* tscpu_printk("XXXXXXXXX\n"); */
  675. if (!strcmp(TEMP, "TEMP")) {
  676. g_trigger_temp = valTEMP;
  677. tscpu_printk("g_trigger_temp=%d\n", valTEMP);
  678. } else {
  679. tscpu_printk("tscpu_write_GPIO_out TEMP bad argument\n");
  680. return -EINVAL;
  681. }
  682. if (!strcmp(ENABLE, "ENABLE")) {
  683. g_GPIO_out_enable = valENABLE;
  684. tscpu_printk("g_GPIO_out_enable=%d,g_GPIO_already_set=%d\n", valENABLE,
  685. g_GPIO_already_set);
  686. } else {
  687. tscpu_printk("tscpu_write_GPIO_out ENABLE bad argument\n");
  688. return -EINVAL;
  689. }
  690. lv_GPIO118_MODE = thermal_readl(GPIO118_MODE);
  691. lv_GPIO118_DIR = thermal_readl(GPIO118_DIR);
  692. /* clear GPIO118_MODE[11:9],GPIO118_MODE = 0 (change to GPIO mode) */
  693. thermal_clrl(GPIO118_MODE, 0x00000E00);
  694. thermal_setl(GPIO118_DIR, 0x00000040); /* set GPIO118_DIR[6]=1,GPIO118_DIR =1 (output) */
  695. thermal_clrl(GPIO118_DOUT, 0x00000040); /* set GPIO118_DOUT[6]=0 Low */
  696. return count;
  697. }
  698. tscpu_printk("tscpu_write_GPIO_out bad argument\n");
  699. return -EINVAL;
  700. }
  701. #endif
  702. static int tscpu_read_opp(struct seq_file *m, void *v)
  703. {
  704. unsigned int cpu_power, gpu_power;
  705. unsigned int gpu_loading = 0;
  706. cpu_power = MIN(adaptive_cpu_power_limit, static_cpu_power_limit);
  707. gpu_power = MIN(adaptive_gpu_power_limit, static_gpu_power_limit);
  708. #if CPT_ADAPTIVE_AP_COOLER
  709. if (!mtk_get_gpu_loading(&gpu_loading))
  710. gpu_loading = 0;
  711. seq_printf(m, "%d,%d,%d,%d,%d\n",
  712. (int)((cpu_power != 0x7FFFFFFF) ? cpu_power : 0),
  713. (int)((gpu_power != 0x7FFFFFFF) ? gpu_power : 0),
  714. /* ((NULL == mtk_thermal_get_gpu_loading_fp) ? 0 : mtk_thermal_get_gpu_loading_fp()), */
  715. (int)gpu_loading, (int)mt_gpufreq_get_cur_freq(), get_target_tj());
  716. #else
  717. seq_printf(m, "%d,%d,0,%d\n",
  718. (int)((cpu_power != 0x7FFFFFFF) ? cpu_power : 0),
  719. (int)((gpu_power != 0x7FFFFFFF) ? gpu_power : 0),
  720. (int)mt_gpufreq_get_cur_freq());
  721. #endif
  722. return 0;
  723. }
  724. static int tscpu_talking_flag_read(struct seq_file *m, void *v)
  725. {
  726. seq_printf(m, "%d\n", talking_flag);
  727. return 0;
  728. }
  729. static ssize_t tscpu_talking_flag_write(struct file *file, const char __user *buffer, size_t count,
  730. loff_t *data)
  731. {
  732. char desc[32];
  733. int lv_talking_flag;
  734. int len = 0;
  735. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  736. if (copy_from_user(desc, buffer, len))
  737. return 0;
  738. desc[len] = '\0';
  739. if (kstrtoint(desc, 10, &lv_talking_flag) == 0) {
  740. talking_flag = lv_talking_flag;
  741. tscpu_dprintk("tscpu_talking_flag_write talking_flag=%d\n", talking_flag);
  742. return count;
  743. }
  744. tscpu_dprintk("tscpu_talking_flag_write bad argument\n");
  745. return -EINVAL;
  746. }
  747. static int tscpu_set_temperature_read(struct seq_file *m, void *v)
  748. {
  749. seq_printf(m, "%d\n", temperature_switch);
  750. return 0;
  751. }
  752. static ssize_t tscpu_set_temperature_write(struct file *file, const char __user *buffer,
  753. size_t count, loff_t *data)
  754. {
  755. char desc[32];
  756. int lv_tempe_switch;
  757. int len = 0;
  758. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  759. if (copy_from_user(desc, buffer, len))
  760. return 0;
  761. desc[len] = '\0';
  762. tscpu_dprintk("tscpu_set_temperature_write\n");
  763. if (kstrtoint(desc, 10, &lv_tempe_switch) == 0) {
  764. temperature_switch = lv_tempe_switch;
  765. tscpu_config_all_tc_hw_protect(temperature_switch, tc_mid_trip);
  766. tscpu_dprintk("tscpu_set_temperature_write temperature_switch=%d\n",
  767. temperature_switch);
  768. return count;
  769. }
  770. tscpu_warn("tscpu_set_temperature_write bad argument\n");
  771. return -EINVAL;
  772. }
  773. static int tscpu_read_log(struct seq_file *m, void *v)
  774. {
  775. seq_printf(m, "[ tscpu_read_log] log = %d\n", tscpu_debug_log);
  776. return 0;
  777. }
  778. static int tscpu_read_cal(struct seq_file *m, void *v)
  779. {
  780. /* seq_printf(m, "mtktscpu cal:\n devinfo index(16)=0x%x, devinfo index(17)=0x%x, devinfo index(18)=0x%x\n", */
  781. /* get_devinfo_with_index(16), get_devinfo_with_index(17), get_devinfo_with_index(18)); */
  782. return 0;
  783. }
  784. static int tscpu_read(struct seq_file *m, void *v)
  785. {
  786. int i;
  787. seq_printf(m,
  788. "[tscpu_read]%d\ntrip_0=%d %d %s\ntrip_1=%d %d %s\ntrip_2=%d %d %s\ntrip_3=%d %d %s\ntrip_4=%d %d %s\ntrip_5=%d %d %s\ntrip_6=%d %d %s\ntrip_7=%d %d %s\ntrip_8=%d %d %s\ntrip_9=%d %d %s\ninterval=%d\n",
  789. num_trip,
  790. trip_temp[0], g_THERMAL_TRIP[0], g_bind0,
  791. trip_temp[1], g_THERMAL_TRIP[1], g_bind1,
  792. trip_temp[2], g_THERMAL_TRIP[2], g_bind2,
  793. trip_temp[3], g_THERMAL_TRIP[3], g_bind3,
  794. trip_temp[4], g_THERMAL_TRIP[4], g_bind4,
  795. trip_temp[5], g_THERMAL_TRIP[5], g_bind5,
  796. trip_temp[6], g_THERMAL_TRIP[6], g_bind6,
  797. trip_temp[7], g_THERMAL_TRIP[7], g_bind7,
  798. trip_temp[8], g_THERMAL_TRIP[8], g_bind8,
  799. trip_temp[9], g_THERMAL_TRIP[9], g_bind9, interval);
  800. for (i = 0; i < Num_of_GPU_OPP; i++)
  801. seq_printf(m, "g %d %d %d\n", i, mtk_gpu_power[i].gpufreq_khz,
  802. mtk_gpu_power[i].gpufreq_power);
  803. for (i = 0; i < tscpu_num_opp; i++)
  804. seq_printf(m, "c %d %d %d %d\n", i, mtk_cpu_power[i].cpufreq_khz,
  805. mtk_cpu_power[i].cpufreq_ncpu, mtk_cpu_power[i].cpufreq_power);
  806. for (i = 0; i < CPU_COOLER_NUM; i++)
  807. seq_printf(m, "d %d %d\n", i, tscpu_cpu_dmips[i]);
  808. return 0;
  809. }
  810. static ssize_t tscpu_write_log(struct file *file, const char __user *buffer, size_t count,
  811. loff_t *data)
  812. {
  813. char desc[32];
  814. int log_switch;
  815. int len = 0;
  816. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  817. if (copy_from_user(desc, buffer, len))
  818. return 0;
  819. desc[len] = '\0';
  820. if (kstrtoint(desc, 10, &log_switch) == 0)
  821. /* if (5 <= sscanf(desc, "%d %d %d %d %d", &log_switch, &hot, &normal, &low, &lv_offset)) */
  822. {
  823. tscpu_debug_log = log_switch;
  824. return count;
  825. }
  826. tscpu_warn("tscpu_write_log bad argument\n");
  827. return -EINVAL;
  828. }
  829. #if MTKTSCPU_FAST_POLLING
  830. static int tscpu_read_fastpoll(struct seq_file *m, void *v)
  831. {
  832. seq_printf(m, "trip %d factor %d\n", fast_polling_trip_temp, fast_polling_factor);
  833. return 0;
  834. }
  835. static ssize_t tscpu_write_fastpoll(struct file *file, const char __user *buffer, size_t count,
  836. loff_t *data)
  837. {
  838. char desc[128];
  839. int len = 0;
  840. int trip = -1, factor = -1;
  841. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  842. if (copy_from_user(desc, buffer, len))
  843. return 0;
  844. desc[len] = '\0';
  845. if (2 <= sscanf(desc, "%d %d", &trip, &factor)) {
  846. tscpu_printk("tscpu_write_fastpoll input %d %d\n", trip, factor);
  847. if ((trip >= 0) && (factor > 0)) {
  848. fast_polling_trip_temp = trip;
  849. fast_polling_factor = factor;
  850. tscpu_printk("tscpu_write_fastpoll applied %d %d\n", fast_polling_trip_temp,
  851. fast_polling_factor);
  852. } else {
  853. tscpu_dprintk("tscpu_write_fastpoll out of range\n");
  854. }
  855. return count;
  856. }
  857. tscpu_dprintk("tscpu_write_fastpoll bad argument\n");
  858. return -EINVAL;
  859. }
  860. #endif
  861. static ssize_t tscpu_write(struct file *file, const char __user *buffer, size_t count,
  862. loff_t *data)
  863. {
  864. int len = 0;
  865. int i;
  866. struct mtktscpu_data {
  867. int trip[10];
  868. int t_type[10];
  869. char bind0[20], bind1[20], bind2[20], bind3[20], bind4[20];
  870. char bind5[20], bind6[20], bind7[20], bind8[20], bind9[20];
  871. int time_msec;
  872. char desc[512];
  873. };
  874. struct mtktscpu_data *ptr_mtktscpu_data = kmalloc(sizeof(*ptr_mtktscpu_data), GFP_KERNEL);
  875. if (ptr_mtktscpu_data == NULL) {
  876. pr_warn("[%s] kmalloc fail\n\n", __func__);
  877. return -ENOMEM;
  878. }
  879. len = (count < (sizeof(ptr_mtktscpu_data->desc) - 1)) ? count : (sizeof(ptr_mtktscpu_data->desc) - 1);
  880. if (copy_from_user(ptr_mtktscpu_data->desc, buffer, len)) {
  881. kfree(ptr_mtktscpu_data);
  882. return 0;
  883. }
  884. ptr_mtktscpu_data->desc[len] = '\0';
  885. if (sscanf
  886. (ptr_mtktscpu_data->desc,
  887. "%d %d %d %19s %d %d %19s %d %d %19s %d %d %19s %d %d %19s %d %d %19s %d %d %19s %d %d %19s %d %d %19s %d %d %19s %d",
  888. &num_trip,
  889. &ptr_mtktscpu_data->trip[0], &ptr_mtktscpu_data->t_type[0], ptr_mtktscpu_data->bind0,
  890. &ptr_mtktscpu_data->trip[1], &ptr_mtktscpu_data->t_type[1], ptr_mtktscpu_data->bind1,
  891. &ptr_mtktscpu_data->trip[2], &ptr_mtktscpu_data->t_type[2], ptr_mtktscpu_data->bind2,
  892. &ptr_mtktscpu_data->trip[3], &ptr_mtktscpu_data->t_type[3], ptr_mtktscpu_data->bind3,
  893. &ptr_mtktscpu_data->trip[4], &ptr_mtktscpu_data->t_type[4], ptr_mtktscpu_data->bind4,
  894. &ptr_mtktscpu_data->trip[5], &ptr_mtktscpu_data->t_type[5], ptr_mtktscpu_data->bind5,
  895. &ptr_mtktscpu_data->trip[6], &ptr_mtktscpu_data->t_type[6], ptr_mtktscpu_data->bind6,
  896. &ptr_mtktscpu_data->trip[7], &ptr_mtktscpu_data->t_type[7], ptr_mtktscpu_data->bind7,
  897. &ptr_mtktscpu_data->trip[8], &ptr_mtktscpu_data->t_type[8], ptr_mtktscpu_data->bind8,
  898. &ptr_mtktscpu_data->trip[9], &ptr_mtktscpu_data->t_type[9], ptr_mtktscpu_data->bind9,
  899. &ptr_mtktscpu_data->time_msec) == 32) {
  900. tscpu_dprintk("tscpu_write tscpu_unregister_thermal MA_len_temp=%d\n", MA_len_temp);
  901. /* modify for PTPOD, if disable Thermal,
  902. PTPOD still need to use this function for getting temperature
  903. */
  904. tscpu_unregister_thermal();
  905. if (num_trip < 0 || num_trip > 10) {
  906. aee_kernel_warning_api(__FILE__, __LINE__, DB_OPT_DEFAULT, "tscpu_write",
  907. "Bad argument");
  908. tscpu_dprintk("tscpu_write bad argument\n");
  909. kfree(ptr_mtktscpu_data);
  910. return -EINVAL;
  911. }
  912. for (i = 0; i < num_trip; i++)
  913. g_THERMAL_TRIP[i] = ptr_mtktscpu_data->t_type[i];
  914. g_bind0[0] = g_bind1[0] = g_bind2[0] = g_bind3[0] = g_bind4[0] = g_bind5[0] =
  915. g_bind6[0] = g_bind7[0] = g_bind8[0] = g_bind9[0] = '\0';
  916. for (i = 0; i < 20; i++) {
  917. g_bind0[i] = ptr_mtktscpu_data->bind0[i];
  918. g_bind1[i] = ptr_mtktscpu_data->bind1[i];
  919. g_bind2[i] = ptr_mtktscpu_data->bind2[i];
  920. g_bind3[i] = ptr_mtktscpu_data->bind3[i];
  921. g_bind4[i] = ptr_mtktscpu_data->bind4[i];
  922. g_bind5[i] = ptr_mtktscpu_data->bind5[i];
  923. g_bind6[i] = ptr_mtktscpu_data->bind6[i];
  924. g_bind7[i] = ptr_mtktscpu_data->bind7[i];
  925. g_bind8[i] = ptr_mtktscpu_data->bind8[i];
  926. g_bind9[i] = ptr_mtktscpu_data->bind9[i];
  927. }
  928. #if CPT_ADAPTIVE_AP_COOLER
  929. /* initialize... */
  930. for (i = 0; i < MAX_CPT_ADAPTIVE_COOLERS; i++)
  931. TARGET_TJS[i] = 117000;
  932. if (!strncmp(&ptr_mtktscpu_data->bind0[0], adaptive_cooler_name, 13))
  933. if ((ptr_mtktscpu_data->bind0[13] - '0') >= 0 &&
  934. (ptr_mtktscpu_data->bind0[13] - '0') < MAX_CPT_ADAPTIVE_COOLERS)
  935. TARGET_TJS[(ptr_mtktscpu_data->bind0[13] - '0')] = ptr_mtktscpu_data->trip[0];
  936. if (!strncmp(&ptr_mtktscpu_data->bind1[0], adaptive_cooler_name, 13))
  937. if ((ptr_mtktscpu_data->bind1[13] - '0') >= 0 &&
  938. (ptr_mtktscpu_data->bind1[13] - '0') < MAX_CPT_ADAPTIVE_COOLERS)
  939. TARGET_TJS[(ptr_mtktscpu_data->bind1[13] - '0')] = ptr_mtktscpu_data->trip[1];
  940. if (!strncmp(&ptr_mtktscpu_data->bind2[0], adaptive_cooler_name, 13))
  941. if ((ptr_mtktscpu_data->bind2[13] - '0') >= 0 &&
  942. (ptr_mtktscpu_data->bind2[13] - '0') < MAX_CPT_ADAPTIVE_COOLERS)
  943. TARGET_TJS[(ptr_mtktscpu_data->bind2[13] - '0')] = ptr_mtktscpu_data->trip[2];
  944. if (!strncmp(&ptr_mtktscpu_data->bind3[0], adaptive_cooler_name, 13))
  945. if ((ptr_mtktscpu_data->bind3[13] - '0') >= 0 &&
  946. (ptr_mtktscpu_data->bind3[13] - '0') < MAX_CPT_ADAPTIVE_COOLERS)
  947. TARGET_TJS[(ptr_mtktscpu_data->bind3[13] - '0')] = ptr_mtktscpu_data->trip[3];
  948. if (!strncmp(&ptr_mtktscpu_data->bind4[0], adaptive_cooler_name, 13))
  949. if ((ptr_mtktscpu_data->bind4[13] - '0') >= 0 &&
  950. (ptr_mtktscpu_data->bind4[13] - '0') < MAX_CPT_ADAPTIVE_COOLERS)
  951. TARGET_TJS[(ptr_mtktscpu_data->bind4[13] - '0')] = ptr_mtktscpu_data->trip[4];
  952. if (!strncmp(&ptr_mtktscpu_data->bind5[0], adaptive_cooler_name, 13))
  953. if ((ptr_mtktscpu_data->bind5[13] - '0') >= 0 &&
  954. (ptr_mtktscpu_data->bind5[13] - '0') < MAX_CPT_ADAPTIVE_COOLERS)
  955. TARGET_TJS[(ptr_mtktscpu_data->bind5[13] - '0')] = ptr_mtktscpu_data->trip[5];
  956. if (!strncmp(&ptr_mtktscpu_data->bind6[0], adaptive_cooler_name, 13))
  957. if ((ptr_mtktscpu_data->bind6[13] - '0') >= 0 &&
  958. (ptr_mtktscpu_data->bind6[13] - '0') < MAX_CPT_ADAPTIVE_COOLERS)
  959. TARGET_TJS[(ptr_mtktscpu_data->bind6[13] - '0')] = ptr_mtktscpu_data->trip[6];
  960. if (!strncmp(&ptr_mtktscpu_data->bind7[0], adaptive_cooler_name, 13))
  961. if ((ptr_mtktscpu_data->bind7[13] - '0') >= 0 &&
  962. (ptr_mtktscpu_data->bind7[13] - '0') < MAX_CPT_ADAPTIVE_COOLERS)
  963. TARGET_TJS[(ptr_mtktscpu_data->bind7[13] - '0')] = ptr_mtktscpu_data->trip[7];
  964. if (!strncmp(&ptr_mtktscpu_data->bind8[0], adaptive_cooler_name, 13))
  965. if ((ptr_mtktscpu_data->bind8[13] - '0') >= 0 &&
  966. (ptr_mtktscpu_data->bind8[13] - '0') < MAX_CPT_ADAPTIVE_COOLERS)
  967. TARGET_TJS[(ptr_mtktscpu_data->bind8[13] - '0')] = ptr_mtktscpu_data->trip[8];
  968. if (!strncmp(&ptr_mtktscpu_data->bind9[0], adaptive_cooler_name, 13))
  969. if ((ptr_mtktscpu_data->bind9[13] - '0') >= 0 &&
  970. (ptr_mtktscpu_data->bind9[13] - '0') < MAX_CPT_ADAPTIVE_COOLERS)
  971. TARGET_TJS[(ptr_mtktscpu_data->bind9[13] - '0')] = ptr_mtktscpu_data->trip[9];
  972. tscpu_dprintk("tscpu_write TTJ0=%d, TTJ1=%d, TTJ2=%d\n", TARGET_TJS[0],
  973. TARGET_TJS[1], TARGET_TJS[2]);
  974. #endif
  975. tscpu_dprintk("tscpu_write g_THERMAL_TRIP_0=%d,g_THERMAL_TRIP_1=%d,g_THERMAL_TRIP_2=%d,",
  976. g_THERMAL_TRIP[0], g_THERMAL_TRIP[1], g_THERMAL_TRIP[2]);
  977. tscpu_dprintk("g_THERMAL_TRIP_3=%d,g_THERMAL_TRIP_4=%d,g_THERMAL_TRIP_5=%d,g_THERMAL_TRIP_6=%d,",
  978. g_THERMAL_TRIP[3], g_THERMAL_TRIP[4], g_THERMAL_TRIP[5], g_THERMAL_TRIP[6]);
  979. tscpu_dprintk("g_THERMAL_TRIP_7=%d,g_THERMAL_TRIP_8=%d,g_THERMAL_TRIP_9=%d,\n",
  980. g_THERMAL_TRIP[7], g_THERMAL_TRIP[8], g_THERMAL_TRIP[9]);
  981. tscpu_dprintk("tscpu_write cooldev0=%s,cooldev1=%s,cooldev2=%s,cooldev3=%s,cooldev4=%s,",
  982. g_bind0, g_bind1, g_bind2, g_bind3, g_bind4);
  983. tscpu_dprintk("cooldev5=%s,cooldev6=%s,cooldev7=%s,cooldev8=%s,cooldev9=%s\n",
  984. g_bind5, g_bind6, g_bind7, g_bind8, g_bind9);
  985. for (i = 0; i < num_trip; i++)
  986. trip_temp[i] = ptr_mtktscpu_data->trip[i];
  987. interval = ptr_mtktscpu_data->time_msec;
  988. tscpu_dprintk("tscpu_write trip_0_temp=%d,trip_1_temp=%d,trip_2_temp=%d,trip_3_temp=%d,trip_4_temp=%d,",
  989. trip_temp[0], trip_temp[1], trip_temp[2], trip_temp[3], trip_temp[4]);
  990. tscpu_dprintk("trip_5_temp=%d,trip_6_temp=%d,trip_7_temp=%d,trip_8_temp=%d,trip_9_temp=%d,",
  991. trip_temp[5], trip_temp[6], trip_temp[7], trip_temp[8], trip_temp[9]);
  992. tscpu_dprintk("time_ms=%d, num_trip=%d\n", interval, num_trip);
  993. /* get temp, set high low threshold */
  994. /*
  995. curr_temp = get_immediate_temp();
  996. for(i=0; i<num_trip; i++)
  997. {
  998. if(curr_temp>trip_temp[i])
  999. break;
  1000. }
  1001. if(i==0)
  1002. {
  1003. tscpu_printk("tscpu_write setting error");
  1004. }
  1005. else if(i==num_trip)
  1006. set_high_low_threshold(trip_temp[i-1], 10000);
  1007. else
  1008. set_high_low_threshold(trip_temp[i-1], trip_temp[i]);
  1009. */
  1010. tscpu_dprintk("tscpu_write tscpu_register_thermal\n");
  1011. tscpu_register_thermal();
  1012. proc_write_flag = 1;
  1013. kfree(ptr_mtktscpu_data);
  1014. return count;
  1015. }
  1016. tscpu_dprintk("tscpu_write bad argument\n");
  1017. aee_kernel_warning_api(__FILE__, __LINE__, DB_OPT_DEFAULT, "tscpu_write",
  1018. "Bad argument");
  1019. kfree(ptr_mtktscpu_data);
  1020. return -EINVAL;
  1021. }
  1022. static int tscpu_register_thermal(void)
  1023. {
  1024. tscpu_dprintk("tscpu_register_thermal\n");
  1025. /* trips : trip 0~3 */
  1026. thz_dev = mtk_thermal_zone_device_register("mtktscpu", num_trip, NULL,
  1027. &mtktscpu_dev_ops, 0, 0, 0, interval);
  1028. return 0;
  1029. }
  1030. static void tscpu_unregister_thermal(void)
  1031. {
  1032. tscpu_dprintk("tscpu_unregister_thermal\n");
  1033. if (thz_dev) {
  1034. mtk_thermal_zone_device_unregister(thz_dev);
  1035. thz_dev = NULL;
  1036. }
  1037. }
  1038. /* pause ALL periodoc temperature sensing point */
  1039. static void thermal_pause_all_periodoc_temp_sensing(void)
  1040. {
  1041. int i = 0;
  1042. unsigned long flags;
  1043. int temp;
  1044. /* tscpu_printk("thermal_pause_all_periodoc_temp_sensing\n"); */
  1045. mt_ptp_lock(&flags);
  1046. /*config bank0,1,2 */
  1047. for (i = 0; i < TS_LEN_ARRAY(tscpu_g_bank); i++) {
  1048. tscpu_switch_bank(i);
  1049. temp = DRV_Reg32(TEMPMSRCTL1);
  1050. /* set bit8=bit1=bit2=bit3=1 to pause sensing point 0,1,2,3 */
  1051. DRV_WriteReg32(TEMPMSRCTL1, (temp | 0x10E));
  1052. }
  1053. mt_ptp_unlock(&flags);
  1054. }
  1055. /* release ALL periodoc temperature sensing point */
  1056. static void thermal_release_all_periodoc_temp_sensing(void)
  1057. {
  1058. int i = 0;
  1059. unsigned long flags;
  1060. int temp;
  1061. /* tscpu_printk("thermal_release_all_periodoc_temp_sensing\n"); */
  1062. mt_ptp_lock(&flags);
  1063. /*config bank0,1,2 */
  1064. for (i = 0; i < TS_LEN_ARRAY(tscpu_g_bank); i++) {
  1065. tscpu_switch_bank(i);
  1066. temp = DRV_Reg32(TEMPMSRCTL1);
  1067. /* set bit1=bit2=bit3=0 to release sensing point 0,1,2 */
  1068. DRV_WriteReg32(TEMPMSRCTL1, ((temp & (~0x10E))));
  1069. }
  1070. mt_ptp_unlock(&flags);
  1071. }
  1072. void tscpu_thermal_enable_all_periodoc_sensing_point(thermal_bank_name bank_num)
  1073. {
  1074. switch (tscpu_g_bank[bank_num].ts_number) {
  1075. case 1:
  1076. /* enable periodoc temperature sensing point 0 */
  1077. THERMAL_WRAP_WR32(0x00000001, TEMPMONCTL0);
  1078. break;
  1079. case 2:
  1080. /* enable periodoc temperature sensing point 0,1 */
  1081. THERMAL_WRAP_WR32(0x00000003, TEMPMONCTL0);
  1082. break;
  1083. case 3:
  1084. /* enable periodoc temperature sensing point 0,1,2 */
  1085. THERMAL_WRAP_WR32(0x00000007, TEMPMONCTL0);
  1086. break;
  1087. default:
  1088. tscpu_printk("Error at %s\n", __func__);
  1089. break;
  1090. }
  1091. }
  1092. /* disable ALL periodoc temperature sensing point */
  1093. static void thermal_disable_all_periodoc_temp_sensing(void)
  1094. {
  1095. int i = 0;
  1096. unsigned long flags;
  1097. /* tscpu_printk("thermal_disable_all_periodoc_temp_sensing\n"); */
  1098. mt_ptp_lock(&flags);
  1099. /*config bank0,1,2 */
  1100. for (i = 0; i < TS_LEN_ARRAY(tscpu_g_bank); i++) {
  1101. tscpu_switch_bank(i);
  1102. /* tscpu_printk("thermal_disable_all_periodoc_temp_sensing:Bank_%d\n",i); */
  1103. THERMAL_WRAP_WR32(0x00000000, TEMPMONCTL0);
  1104. }
  1105. mt_ptp_unlock(&flags);
  1106. }
  1107. static void tscpu_clear_all_temp(void)
  1108. {
  1109. /* 26111 to avoid ptpod judge <25deg will not update voltage. */
  1110. /* CPU_TS_MCU2_T=26111; */
  1111. /* GPU_TS_MCU1_T=26111; */
  1112. /* LTE_TS_MCU3_T=26111; */
  1113. int i = 0;
  1114. int j = 0;
  1115. for (i = 0; i < TS_LEN_ARRAY(tscpu_g_bank); i++) {
  1116. for (j = 0; j < tscpu_g_bank[i].ts_number; j++)
  1117. tscpu_bank_ts[i][tscpu_g_bank[i].ts[j].type] = CLEAR_TEMP;
  1118. }
  1119. }
  1120. /*tscpu_thermal_suspend spend 1000us~1310us*/
  1121. static int tscpu_thermal_suspend(struct platform_device *dev, pm_message_t state)
  1122. {
  1123. int cnt = 0;
  1124. int temp = 0;
  1125. tscpu_printk("tscpu_thermal_suspend\n");
  1126. #if THERMAL_PERFORMANCE_PROFILE
  1127. struct timeval begin, end;
  1128. unsigned long val;
  1129. do_gettimeofday(&begin);
  1130. #endif
  1131. g_tc_resume = 1; /* set "1", don't read temp during suspend */
  1132. if (talking_flag == false) {
  1133. tscpu_dprintk("tscpu_thermal_suspend no talking\n");
  1134. #if (CONFIG_THERMAL_AEE_RR_REC == 1)
  1135. aee_rr_rec_thermal_status(TSCPU_SUSPEND);
  1136. #endif
  1137. while (cnt < 50) {
  1138. temp = (DRV_Reg32(THAHBST0) >> 16);
  1139. if (cnt > 10)
  1140. pr_debug("THAHBST0 = 0x%x,cnt=%d, %d\n", temp, cnt,
  1141. __LINE__);
  1142. if (temp == 0x0) {
  1143. /* pause all periodoc temperature sensing point 0~2 */
  1144. thermal_pause_all_periodoc_temp_sensing(); /* TEMPMSRCTL1 */
  1145. break;
  1146. }
  1147. udelay(2);
  1148. cnt++;
  1149. }
  1150. /* disable periodic temp measurement on sensor 0~2 */
  1151. thermal_disable_all_periodoc_temp_sensing(); /* TEMPMONCTL0 */
  1152. /* tscpu_thermal_clock_off(); */
  1153. /*TSCON1[5:4]=2'b11, Buffer off */
  1154. /* turn off the sensor buffer to save power */
  1155. THERMAL_WRAP_WR32(DRV_Reg32(TS_CONFIGURE) | TS_TURN_OFF, TS_CONFIGURE);
  1156. }
  1157. #if THERMAL_PERFORMANCE_PROFILE
  1158. do_gettimeofday(&end);
  1159. /* Get milliseconds */
  1160. pr_debug("suspend time spent, sec : %lu , usec : %lu\n", (end.tv_sec - begin.tv_sec),
  1161. (end.tv_usec - begin.tv_usec));
  1162. #endif
  1163. return 0;
  1164. }
  1165. /*tscpu_thermal_suspend spend 3000us~4000us*/
  1166. static int tscpu_thermal_resume(struct platform_device *dev)
  1167. {
  1168. int temp = 0;
  1169. int cnt = 0;
  1170. tscpu_printk("tscpu_thermal_resume\n");
  1171. g_tc_resume = 1; /* set "1", don't read temp during start resume */
  1172. if (talking_flag == false) {
  1173. #if (CONFIG_THERMAL_AEE_RR_REC == 1)
  1174. aee_rr_rec_thermal_status(TSCPU_RESUME);
  1175. #endif
  1176. tscpu_reset_thermal();
  1177. temp = DRV_Reg32(TS_CONFIGURE);
  1178. temp &= ~(TS_TURN_OFF); /* TS_CON1[5:4]=2'b00, 00: Buffer on, TSMCU to AUXADC */
  1179. THERMAL_WRAP_WR32(temp, TS_CONFIGURE); /* read abb need */
  1180. /* RG_TS2AUXADC < set from 2'b11 to 2'b00
  1181. when resume.wait 100uS than turn on thermal controller. */
  1182. udelay(200);
  1183. /*add this function to read all temp first to avoid
  1184. write TEMPPROTTC first time will issue an fake signal to RGU */
  1185. tscpu_fast_initial_sw_workaround();
  1186. while (cnt < 50) {
  1187. temp = (DRV_Reg32(THAHBST0) >> 16);
  1188. if (cnt > 10)
  1189. pr_debug("THAHBST0 = 0x%x,cnt=%d, %d\n", temp, cnt, __LINE__);
  1190. if (temp == 0x0) {
  1191. /* pause all periodoc temperature sensing point 0~2 */
  1192. thermal_pause_all_periodoc_temp_sensing(); /* TEMPMSRCTL1 */
  1193. break;
  1194. }
  1195. udelay(2);
  1196. cnt++;
  1197. }
  1198. thermal_disable_all_periodoc_temp_sensing(); /* TEMPMONCTL0 */
  1199. tscpu_thermal_initial_all_bank();
  1200. thermal_release_all_periodoc_temp_sensing(); /* must release before start */
  1201. tscpu_clear_all_temp();
  1202. tscpu_config_all_tc_hw_protect(trip_temp[0], tc_mid_trip);
  1203. }
  1204. g_tc_resume = 2; /* set "2", resume finish,can read temp */
  1205. return 0;
  1206. }
  1207. static struct platform_driver mtk_thermal_driver = {
  1208. .remove = NULL,
  1209. .shutdown = NULL,
  1210. .probe = tscpu_thermal_probe,
  1211. .suspend = tscpu_thermal_suspend,
  1212. .resume = tscpu_thermal_resume,
  1213. .driver = {
  1214. .name = THERMAL_NAME,
  1215. #ifdef CONFIG_OF
  1216. .of_match_table = mt_thermal_of_match,
  1217. #endif
  1218. },
  1219. };
  1220. #if MTK_TS_CPU_RT
  1221. static int ktp_limited = -275000;
  1222. static int ktp_thread(void *arg)
  1223. {
  1224. int max_temp = 0;
  1225. int bank0_T;
  1226. struct sched_param param = {.sched_priority = 98 };
  1227. sched_setscheduler(current, SCHED_FIFO, &param);
  1228. set_current_state(TASK_INTERRUPTIBLE);
  1229. tscpu_printk("ktp_thread 1st run\n");
  1230. schedule();
  1231. for (;;) {
  1232. int temp_tc_mid_trip = tc_mid_trip;
  1233. int temp_ktp_limited = ktp_limited;
  1234. tscpu_printk("ktp_thread awake,tc_mid_trip=%d\n", tc_mid_trip);
  1235. if (kthread_should_stop())
  1236. break;
  1237. /* bank0_T = MAX(MAX(CPU_TS_MCU2_T,GPU_TS_MCU1_T),LTE_TS_MCU3_T); */
  1238. bank0_T = tscpu_max_temperature();
  1239. max_temp = bank0_T;
  1240. tscpu_warn("ktp_thread temp=%d\n", max_temp);
  1241. if ((temp_tc_mid_trip > -275000) && (max_temp >= (temp_tc_mid_trip - 5000))) {
  1242. /* trip_temp[1] should be shutdown point... */
  1243. /* Do what ever we want */
  1244. tscpu_dprintk("ktp_thread overheat %d\n", max_temp);
  1245. /* freq/volt down or cpu down or backlight down or charging down... */
  1246. #if defined(CONFIG_ARCH_MT6755)
  1247. mt_ppm_cpu_thermal_protect(600); /*D1 max~1600mW,min~600 */
  1248. #else
  1249. mt_cpufreq_thermal_protect(600); /*D1 max~1600mW,min~600 */
  1250. #endif
  1251. mt_gpufreq_thermal_protect(600); /*D1 max~900mW,min~600mW */
  1252. ktp_limited = temp_tc_mid_trip;
  1253. msleep(20 * 1000);
  1254. } else if ((temp_ktp_limited > -275000) && (max_temp < temp_ktp_limited)) {
  1255. unsigned int final_limit;
  1256. final_limit = MIN(static_cpu_power_limit, adaptive_cpu_power_limit);
  1257. tscpu_dprintk("ktp_thread unlimit cpu=%d\n", final_limit);
  1258. #if defined(CONFIG_ARCH_MT6755)
  1259. mt_ppm_cpu_thermal_protect((final_limit != 0x7FFFFFFF) ? final_limit : 0);
  1260. #else
  1261. mt_cpufreq_thermal_protect((final_limit != 0x7FFFFFFF) ? final_limit : 0);
  1262. #endif
  1263. final_limit = MIN(static_gpu_power_limit, adaptive_gpu_power_limit);
  1264. tscpu_dprintk("ktp_thread unlimit gpu=%d\n", final_limit);
  1265. mt_gpufreq_thermal_protect((final_limit != 0x7FFFFFFF) ? final_limit : 0);
  1266. ktp_limited = -275000;
  1267. set_current_state(TASK_INTERRUPTIBLE);
  1268. schedule();
  1269. } else {
  1270. tscpu_dprintk("ktp_thread else temp=%d, trip=%d, ltd=%d\n", max_temp,
  1271. temp_tc_mid_trip, temp_ktp_limited);
  1272. set_current_state(TASK_INTERRUPTIBLE);
  1273. schedule();
  1274. }
  1275. }
  1276. tscpu_dprintk("ktp_thread stopped\n");
  1277. return 0;
  1278. }
  1279. #endif
  1280. int tscpu_get_temp_by_bank(thermal_bank_name ts_bank)
  1281. {
  1282. int bank_T = 0;
  1283. tscpu_dprintk("tscpu_get_temp %s, %d\n", __func__, __LINE__);
  1284. if (ts_bank < TS_LEN_ARRAY(tscpu_g_bank))
  1285. bank_T = max_temperature_in_bank(ts_bank);
  1286. else
  1287. panic("Bank number out of range\n");
  1288. return bank_T;
  1289. }
  1290. #if THERMAL_GPIO_OUT_TOGGLE
  1291. static int tscpu_GPIO_out(struct inode *inode, struct file *file)
  1292. {
  1293. return single_open(file, tscpu_read_GPIO_out, NULL);
  1294. }
  1295. static const struct file_operations mtktscpu_GPIO_out_fops = {
  1296. .owner = THIS_MODULE,
  1297. .open = tscpu_GPIO_out,
  1298. .read = seq_read,
  1299. .llseek = seq_lseek,
  1300. .write = tscpu_write_GPIO_out,
  1301. .release = single_release,
  1302. };
  1303. #endif
  1304. static int tscpu_Tj_out(struct inode *inode, struct file *file)
  1305. {
  1306. return single_open(file, tscpu_read_Tj_out, NULL);
  1307. }
  1308. static const struct file_operations mtktscpu_Tj_out_fops = {
  1309. .owner = THIS_MODULE,
  1310. .open = tscpu_Tj_out,
  1311. .read = seq_read,
  1312. .llseek = seq_lseek,
  1313. .write = tscpu_write_Tj_out,
  1314. .release = single_release,
  1315. };
  1316. static int tscpu_open_opp(struct inode *inode, struct file *file)
  1317. {
  1318. return single_open(file, tscpu_read_opp, NULL);
  1319. }
  1320. static const struct file_operations mtktscpu_opp_fops = {
  1321. .owner = THIS_MODULE,
  1322. .open = tscpu_open_opp,
  1323. .read = seq_read,
  1324. .llseek = seq_lseek,
  1325. .release = single_release,
  1326. };
  1327. static int tscpu_open_log(struct inode *inode, struct file *file)
  1328. {
  1329. return single_open(file, tscpu_read_log, NULL);
  1330. }
  1331. static const struct file_operations mtktscpu_log_fops = {
  1332. .owner = THIS_MODULE,
  1333. .open = tscpu_open_log,
  1334. .read = seq_read,
  1335. .llseek = seq_lseek,
  1336. .write = tscpu_write_log,
  1337. .release = single_release,
  1338. };
  1339. static int tscpu_open(struct inode *inode, struct file *file)
  1340. {
  1341. return single_open(file, tscpu_read, NULL);
  1342. }
  1343. static const struct file_operations mtktscpu_fops = {
  1344. .owner = THIS_MODULE,
  1345. .open = tscpu_open,
  1346. .read = seq_read,
  1347. .llseek = seq_lseek,
  1348. .write = tscpu_write,
  1349. .release = single_release,
  1350. };
  1351. #if defined(CONFIG_ARCH_MT6753)
  1352. /*For MT6753 PMIC 5A throttle patch*/
  1353. static int tzcpu_cpufreq5A_read(struct seq_file *m, void *v)
  1354. {
  1355. seq_printf(m, "Thermal 5A threshold= %d\n", thermal5A_TH);
  1356. return 0;
  1357. }
  1358. static ssize_t tzcpu_cpufreq5A_write(struct file *file, const char __user *buffer, size_t count, loff_t *data)
  1359. {
  1360. char desc[32];
  1361. int th;
  1362. int len = 0;
  1363. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  1364. if (copy_from_user(desc, buffer, len))
  1365. return 0;
  1366. desc[len] = '\0';
  1367. if (kstrtoint(desc, 10, &th) == 0) {
  1368. thermal5A_TH = th;
  1369. return count;
  1370. }
  1371. tscpu_printk(" bad argument\n");
  1372. return -EINVAL;
  1373. }
  1374. static int tzcpu_cpufreq5A_open(struct inode *inode, struct file *file)
  1375. {
  1376. return single_open(file, tzcpu_cpufreq5A_read, NULL);
  1377. }
  1378. static const struct file_operations tzcpu_cpufreq5A_fops = {
  1379. .owner = THIS_MODULE,
  1380. .open = tzcpu_cpufreq5A_open,
  1381. .read = seq_read,
  1382. .llseek = seq_lseek,
  1383. .write = tzcpu_cpufreq5A_write,
  1384. .release = single_release,
  1385. };
  1386. #endif
  1387. static int tscpu_cal_open(struct inode *inode, struct file *file)
  1388. {
  1389. return single_open(file, tscpu_read_cal, NULL);
  1390. }
  1391. static const struct file_operations mtktscpu_cal_fops = {
  1392. .owner = THIS_MODULE,
  1393. .open = tscpu_cal_open,
  1394. .read = seq_read,
  1395. .llseek = seq_lseek,
  1396. .release = single_release,
  1397. };
  1398. static int tscpu_read_temperature_open(struct inode *inode, struct file *file)
  1399. {
  1400. return single_open(file, tscpu_read_temperature_info, NULL);
  1401. }
  1402. static const struct file_operations mtktscpu_read_temperature_fops = {
  1403. .owner = THIS_MODULE,
  1404. .open = tscpu_read_temperature_open,
  1405. .read = seq_read,
  1406. .llseek = seq_lseek,
  1407. .write = tscpu_write,
  1408. .release = single_release,
  1409. };
  1410. static int tscpu_set_temperature_open(struct inode *inode, struct file *file)
  1411. {
  1412. return single_open(file, tscpu_set_temperature_read, NULL);
  1413. }
  1414. static const struct file_operations mtktscpu_set_temperature_fops = {
  1415. .owner = THIS_MODULE,
  1416. .open = tscpu_set_temperature_open,
  1417. .read = seq_read,
  1418. .llseek = seq_lseek,
  1419. .write = tscpu_set_temperature_write,
  1420. .release = single_release,
  1421. };
  1422. static int tscpu_talking_flag_open(struct inode *inode, struct file *file)
  1423. {
  1424. return single_open(file, tscpu_talking_flag_read, NULL);
  1425. }
  1426. static const struct file_operations mtktscpu_talking_flag_fops = {
  1427. .owner = THIS_MODULE,
  1428. .open = tscpu_talking_flag_open,
  1429. .read = seq_read,
  1430. .llseek = seq_lseek,
  1431. .write = tscpu_talking_flag_write,
  1432. .release = single_release,
  1433. };
  1434. #if MTKTSCPU_FAST_POLLING
  1435. static int tscpu_fastpoll_open(struct inode *inode, struct file *file)
  1436. {
  1437. return single_open(file, tscpu_read_fastpoll, NULL);
  1438. }
  1439. static const struct file_operations mtktscpu_fastpoll_fops = {
  1440. .owner = THIS_MODULE,
  1441. .open = tscpu_fastpoll_open,
  1442. .read = seq_read,
  1443. .llseek = seq_lseek,
  1444. .write = tscpu_write_fastpoll,
  1445. .release = single_release,
  1446. };
  1447. #endif
  1448. #if THERMAL_DRV_UPDATE_TEMP_DIRECT_TO_MET
  1449. int tscpu_get_cpu_temp_met(MTK_THERMAL_SENSOR_CPU_ID_MET id)
  1450. {
  1451. unsigned long flags;
  1452. int ret;
  1453. if (id < 0 || id >= MTK_THERMAL_SENSOR_CPU_COUNT)
  1454. return -127000;
  1455. if (ATM_CPU_LIMIT == id)
  1456. return (adaptive_cpu_power_limit != 0x7FFFFFFF) ? adaptive_cpu_power_limit : 0;
  1457. if (ATM_GPU_LIMIT == id)
  1458. return (adaptive_gpu_power_limit != 0x7FFFFFFF) ? adaptive_gpu_power_limit : 0;
  1459. tscpu_met_lock(&flags);
  1460. if (a_tscpu_all_temp[id] == 0) {
  1461. tscpu_met_unlock(&flags);
  1462. return -127000;
  1463. }
  1464. ret = a_tscpu_all_temp[id];
  1465. tscpu_met_unlock(&flags);
  1466. return ret;
  1467. }
  1468. EXPORT_SYMBOL(tscpu_get_cpu_temp_met);
  1469. #endif
  1470. #if defined(CONFIG_ARCH_MT6755)
  1471. #if 0
  1472. static int thermal_auxadc_get_data(int times, int channel)
  1473. {
  1474. int ret = 0, data[4], i, ret_value = 0, ret_temp = 0;
  1475. pr_err("Thermal_auxadc_get_data\n");
  1476. if (IMM_IsAdcInitReady() == 0) {
  1477. pr_err("[thermal_auxadc_get_data]: AUXADC is not ready\n");
  1478. return 0;
  1479. }
  1480. for (i = 0; i < times; i++) {
  1481. ret_value = IMM_GetOneChannelValue(channel, data, &ret_temp);
  1482. pr_err("[thermal_auxadc_get_data]: raw%d= %d\n", i, ret_temp);
  1483. ret += ret_temp;
  1484. }
  1485. ret = ret / times;
  1486. return ret;
  1487. }
  1488. #endif
  1489. /*Patch to pause thermal controller and turn off auxadc GC.
  1490. For mt6755 only*/
  1491. #if 1
  1492. static void tscpu_thermal_pause(void)
  1493. {
  1494. int cnt = 0;
  1495. int temp = 0;
  1496. aee_rr_rec_thermal_status(TSCPU_PAUSE);
  1497. thermal_pause_all_periodoc_temp_sensing(); /* TEMPMSRCTL1 */
  1498. do {
  1499. temp = (DRV_Reg32(THAHBST0) >> 16);
  1500. if (cnt > 10)
  1501. pr_err("THAHBST0 = 0x%x, cnt = %d, %d\n", temp, cnt, __LINE__);
  1502. udelay(2);
  1503. cnt++;
  1504. } while (temp != 0x0 && cnt < 50);
  1505. /* disable periodic temp measurement on sensor 0~2 */
  1506. thermal_disable_all_periodoc_temp_sensing(); /* TEMPMONCTL0 */
  1507. #if !defined(CONFIG_MTK_CLKMGR)
  1508. if (therm_auxadc)
  1509. clk_disable_unprepare(therm_auxadc);
  1510. #endif
  1511. }
  1512. static void tscpu_thermal_release(void)
  1513. {
  1514. int temp = 0;
  1515. int cnt = 0;
  1516. aee_rr_rec_thermal_status(TSCPU_RELEASE);
  1517. #if !defined(CONFIG_MTK_CLKMGR)
  1518. if (therm_auxadc)
  1519. clk_prepare_enable(therm_auxadc);
  1520. #endif
  1521. /*thermal_auxadc_get_data(2, 11);*/
  1522. thermal_release_all_periodoc_temp_sensing(); /* must release before start */
  1523. tscpu_fast_initial_sw_workaround();
  1524. thermal_pause_all_periodoc_temp_sensing(); /* TEMPMSRCTL1 */
  1525. do {
  1526. temp = (DRV_Reg32(THAHBST0) >> 16);
  1527. if (cnt > 10)
  1528. pr_err("THAHBST0 = 0x%x, cnt = %d, %d\n", temp, cnt, __LINE__);
  1529. udelay(2);
  1530. cnt++;
  1531. } while (temp != 0x0 && cnt < 50);
  1532. thermal_disable_all_periodoc_temp_sensing(); /* TEMPMONCTL0 */
  1533. tscpu_thermal_initial_all_bank();
  1534. thermal_release_all_periodoc_temp_sensing(); /* must release before start */
  1535. }
  1536. #else
  1537. static void tscpu_thermal_pause(void)
  1538. {
  1539. int cnt = 0, temp = 0;
  1540. aee_rr_rec_thermal_status(TSCPU_PAUSE);
  1541. thermal_pause_all_periodoc_temp_sensing(); /* TEMPMSRCTL1 */
  1542. thermal_disable_all_periodoc_temp_sensing(); /* TEMPMONCTL0 */
  1543. while (temp != 0x0 && cnt < 50) {
  1544. temp = (DRV_Reg32(THAHBST0) >> 16);
  1545. if (cnt > 10)
  1546. tscpu_printk("THAHBST0 = 0x%x, cnt = %d, %d\n", temp, cnt, __LINE__);
  1547. udelay(2);
  1548. cnt++;
  1549. }
  1550. #if !defined(CONFIG_MTK_CLKMGR)
  1551. if (therm_auxadc)
  1552. clk_disable_unprepare(therm_auxadc);
  1553. #endif
  1554. }
  1555. static void tscpu_thermal_release(void)
  1556. {
  1557. int i = 0;
  1558. unsigned long flags;
  1559. aee_rr_rec_thermal_status(TSCPU_RELEASE);
  1560. #if !defined(CONFIG_MTK_CLKMGR)
  1561. if (therm_auxadc)
  1562. clk_prepare_enable(therm_auxadc);
  1563. #endif
  1564. thermal_release_all_periodoc_temp_sensing(); /* must release before start */
  1565. mt_ptp_lock(&flags);
  1566. for (i = 0; i < TS_LEN_ARRAY(tscpu_g_bank); i++) {
  1567. tscpu_switch_bank(i);
  1568. tscpu_thermal_enable_all_periodoc_sensing_point(i);
  1569. }
  1570. mt_ptp_unlock(&flags);
  1571. }
  1572. #endif
  1573. #endif
  1574. static DEFINE_SPINLOCK(temp_valid_spinlock);
  1575. static void temp_valid_lock(unsigned long *flags)
  1576. {
  1577. spin_lock_irqsave(&temp_valid_spinlock, *flags);
  1578. }
  1579. static void temp_valid_unlock(unsigned long *flags)
  1580. {
  1581. spin_unlock_irqrestore(&temp_valid_spinlock, *flags);
  1582. }
  1583. static void check_all_temp_valid(void)
  1584. {
  1585. int i, j, raw;
  1586. for (i = 0; i < ARRAY_SIZE(tscpu_g_bank); i++) {
  1587. for (j = 0; j < tscpu_g_bank[i].ts_number; j++) {
  1588. raw = tscpu_bank_ts_r[i][tscpu_g_bank[i].ts[j].type];
  1589. if (raw == THERMAL_INIT_VALUE)
  1590. return; /* The temperature is not valid. */
  1591. }
  1592. }
  1593. g_is_temp_valid = 1;
  1594. }
  1595. int tscpu_is_temp_valid(void)
  1596. {
  1597. int is_valid = 0;
  1598. unsigned long flags;
  1599. temp_valid_lock(&flags);
  1600. if (g_is_temp_valid == 0)
  1601. check_all_temp_valid();
  1602. is_valid = g_is_temp_valid;
  1603. temp_valid_unlock(&flags);
  1604. return is_valid;
  1605. }
  1606. static void read_all_bank_temperature(void)
  1607. {
  1608. int i = 0;
  1609. int j = 0;
  1610. unsigned long flags;
  1611. mt_ptp_lock(&flags);
  1612. for (i = 0; i < TS_LEN_ARRAY(tscpu_g_bank); i++) {
  1613. tscpu_switch_bank(i);
  1614. for (j = 0; j < tscpu_g_bank[i].ts_number; j++)
  1615. tscpu_thermal_read_bank_temp(i, tscpu_g_bank[i].ts[j].type, j);
  1616. }
  1617. mt_ptp_unlock(&flags);
  1618. tscpu_is_temp_valid();
  1619. }
  1620. void tscpu_update_tempinfo(void)
  1621. {
  1622. unsigned long flags;
  1623. if (g_tc_resume == 0)
  1624. read_all_bank_temperature();
  1625. else if (g_tc_resume == 2) /* resume ready */
  1626. g_tc_resume = 0;
  1627. #if (CONFIG_THERMAL_AEE_RR_REC == 1)
  1628. aee_rr_rec_thermal_temp1(get_immediate_ts1_wrap() / 1000);
  1629. aee_rr_rec_thermal_temp2(get_immediate_ts2_wrap() / 1000);
  1630. aee_rr_rec_thermal_temp3(get_immediate_ts3_wrap() / 1000);
  1631. aee_rr_rec_thermal_temp4(get_immediate_ts4_wrap() / 1000);
  1632. aee_rr_rec_thermal_temp5(get_immediate_tsabb_wrap() / 1000);
  1633. aee_rr_rec_thermal_status(TSCPU_NORMAL);
  1634. #endif
  1635. #if THERMAL_DRV_UPDATE_TEMP_DIRECT_TO_MET
  1636. tscpu_met_lock(&flags);
  1637. tscpu_dprintk("tscpu_get_temp %s, %d\n", __func__, __LINE__);
  1638. a_tscpu_all_temp[MTK_THERMAL_SENSOR_TS1] = get_immediate_ts1_wrap();
  1639. a_tscpu_all_temp[MTK_THERMAL_SENSOR_TS2] = get_immediate_ts2_wrap();
  1640. a_tscpu_all_temp[MTK_THERMAL_SENSOR_TS3] = get_immediate_ts3_wrap();
  1641. a_tscpu_all_temp[MTK_THERMAL_SENSOR_TS4] = get_immediate_ts4_wrap();
  1642. a_tscpu_all_temp[MTK_THERMAL_SENSOR_TSABB] = get_immediate_tsabb_wrap();
  1643. tscpu_met_unlock(&flags);
  1644. if (NULL != g_pThermalSampler)
  1645. g_pThermalSampler();
  1646. #endif
  1647. #if 0
  1648. pr_debug("\n\n");
  1649. \\tscpu_printk("\n tscpu_update_tempinfo, T=%d,%d,%d,%d,%d,%d\n", CPU_TS_MCU1_T,
  1650. CPU_TS_MCU2_T, GPU_TS_MCU3_T, SOC_TS_MCU4_T, SOC_TS_MCU2_T, SOC_TS_MCU3_T);
  1651. pr_debug("Bank 0 : CPU (TS_MCU1 = %d,TS_MCU2 = %d)\n", CPU_TS_MCU1_T,
  1652. CPU_TS_MCU2_T);
  1653. pr_debug("Bank 1 : GPU (TS_MCU3 = %d)\n", GPU_TS_MCU3_T);
  1654. pr_debug("Bank 2 : SOC (TS_MCU4 = %d,TS_MCU2 = %d,TS_MCU3 = %d)\n", SOC_TS_MCU4_T,
  1655. SOC_TS_MCU2_T, SOC_TS_MCU3_T);
  1656. #endif
  1657. }
  1658. void tscpu_cancel_thermal_timer(void)
  1659. {
  1660. /* stop thermal framework polling when entering deep idle */
  1661. if (thz_dev)
  1662. cancel_delayed_work(&(thz_dev->poll_queue));
  1663. #if defined(CONFIG_ARCH_MT6755)
  1664. /*Patch to pause thermal controller and turn off auxadc GC.
  1665. For mt6755 only*/
  1666. tscpu_thermal_pause();
  1667. #endif
  1668. }
  1669. void tscpu_start_thermal_timer(void)
  1670. {
  1671. /* resume thermal framework polling when leaving deep idle */
  1672. if (thz_dev != NULL && interval != 0)
  1673. mod_delayed_work(system_freezable_wq, &(thz_dev->poll_queue), round_jiffies(msecs_to_jiffies(1000)));
  1674. #if defined(CONFIG_ARCH_MT6755)
  1675. /*Patch to pause thermal controller and turn off auxadc GC.
  1676. For mt6755 only*/
  1677. tscpu_thermal_release();
  1678. #endif
  1679. }
  1680. #ifdef CONFIG_OF
  1681. long tscpu_dev_alloc_module_base_by_name(const char *name)
  1682. {
  1683. unsigned long VA;
  1684. struct device_node *node = NULL;
  1685. node = of_find_compatible_node(NULL, NULL, name);
  1686. if (!node) {
  1687. pr_debug("find node failed\n");
  1688. return 0;
  1689. }
  1690. VA = (unsigned long)of_iomap(node, 0);
  1691. pr_debug("DEV: VA(%s): 0x%lx\n", name, VA);
  1692. return VA;
  1693. }
  1694. #endif
  1695. static void init_thermal(void)
  1696. {
  1697. int temp = 0;
  1698. int cnt = 0;
  1699. #if (CONFIG_THERMAL_AEE_RR_REC == 1)
  1700. _mt_thermal_aee_init();
  1701. aee_rr_rec_thermal_status(TSCPU_INIT);
  1702. #endif
  1703. tscpu_thermal_cal_prepare();
  1704. tscpu_thermal_cal_prepare_2(0);
  1705. tscpu_reset_thermal();
  1706. /* thermal spm verification */
  1707. #if 0
  1708. spm_write(SPM_SLEEP_WAKEUP_EVENT_MASK, ~(1U << 21)); /* unmask bit21 for thermal wake up source */
  1709. tscpu_printk("SPM_SLEEP_WAKEUP_EVENT_MASK =0x%08x\n",
  1710. spm_read(SPM_SLEEP_WAKEUP_EVENT_MASK));
  1711. #endif
  1712. /*
  1713. TS_CON1 default is 0x30, this is buffer off
  1714. we should turn on this buffer berore we use thermal sensor,
  1715. or this buffer off will let TC read a very small value from auxadc
  1716. and this small value will trigger thermal reboot
  1717. */
  1718. temp = DRV_Reg32(TS_CONFIGURE);
  1719. temp &= ~(TS_TURN_OFF); /* TS_CON1[5:4]=2'b00, 00: Buffer on, TSMCU to AUXADC */
  1720. THERMAL_WRAP_WR32(temp, TS_CONFIGURE); /* read abb need */
  1721. /* RG_TS2AUXADC < set from 2'b11 to 2'b00
  1722. when resume.wait 100uS than turn on thermal controller.*/
  1723. udelay(200);
  1724. BUG_ON((DRV_Reg32(TS_CONFIGURE) & TS_TURN_OFF) != 0x0);
  1725. BUG_ON(IMM_IsAdcInitReady() != 1);
  1726. /*add this function to read all temp first to avoid
  1727. write TEMPPROTTC first will issue an fake signal to RGU */
  1728. tscpu_fast_initial_sw_workaround();
  1729. while (cnt < 50) {
  1730. temp = (DRV_Reg32(THAHBST0) >> 16);
  1731. if (cnt > 10)
  1732. pr_debug("THAHBST0 = 0x%x,cnt=%d, %d\n", temp, cnt, __LINE__);
  1733. if (temp == 0x0) {
  1734. /* pause all periodoc temperature sensing point 0~2 */
  1735. thermal_pause_all_periodoc_temp_sensing(); /* TEMPMSRCTL1 */
  1736. break;
  1737. }
  1738. udelay(2);
  1739. cnt++;
  1740. }
  1741. thermal_disable_all_periodoc_temp_sensing(); /* TEMPMONCTL0 */
  1742. /* pr_debug(KERN_CRIT "cnt = %d, %d\n",cnt,__LINE__); */
  1743. /*Normal initial */
  1744. tscpu_thermal_initial_all_bank();
  1745. thermal_release_all_periodoc_temp_sensing(); /* TEMPMSRCTL1 must release before start */
  1746. read_all_bank_temperature();
  1747. }
  1748. static void tscpu_create_fs(void)
  1749. {
  1750. struct proc_dir_entry *entry = NULL;
  1751. struct proc_dir_entry *mtktscpu_dir = NULL;
  1752. mtktscpu_dir = mtk_thermal_get_proc_drv_therm_dir_entry();
  1753. if (!mtktscpu_dir) {
  1754. tscpu_printk("[%s]: mkdir /proc/driver/thermal failed\n", __func__);
  1755. } else {
  1756. entry =
  1757. proc_create("tzcpu", S_IRUGO | S_IWUSR | S_IWGRP, mtktscpu_dir, &mtktscpu_fops);
  1758. if (entry)
  1759. proc_set_user(entry, uid, gid);
  1760. #if defined(CONFIG_ARCH_MT6753)
  1761. /*For MT6753 PMIC 5A throttle patch*/
  1762. entry = proc_create("tzcpu_cpufreq5A", S_IRUGO | S_IWUSR | S_IWGRP,
  1763. mtktscpu_dir, &tzcpu_cpufreq5A_fops);
  1764. if (entry)
  1765. proc_set_user(entry, uid, gid);
  1766. #endif
  1767. entry =
  1768. proc_create("tzcpu_log", S_IRUGO | S_IWUSR, mtktscpu_dir, &mtktscpu_log_fops);
  1769. entry = proc_create("thermlmt", S_IRUGO, NULL, &mtktscpu_opp_fops);
  1770. entry = proc_create("tzcpu_cal", S_IRUSR, mtktscpu_dir, &mtktscpu_cal_fops);
  1771. entry =
  1772. proc_create("tzcpu_read_temperature", S_IRUGO, mtktscpu_dir,
  1773. &mtktscpu_read_temperature_fops);
  1774. entry =
  1775. proc_create("tzcpu_set_temperature", S_IRUGO | S_IWUSR, mtktscpu_dir,
  1776. &mtktscpu_set_temperature_fops);
  1777. entry =
  1778. proc_create("tzcpu_talking_flag", S_IRUGO | S_IWUSR, mtktscpu_dir,
  1779. &mtktscpu_talking_flag_fops);
  1780. #if MTKTSCPU_FAST_POLLING
  1781. entry =
  1782. proc_create("tzcpu_fastpoll", S_IRUGO | S_IWUSR | S_IWGRP, mtktscpu_dir,
  1783. &mtktscpu_fastpoll_fops);
  1784. if (entry)
  1785. proc_set_user(entry, uid, gid);
  1786. #endif /* #if MTKTSCPU_FAST_POLLING */
  1787. entry =
  1788. proc_create("tzcpu_Tj_out_via_HW_pin", S_IRUGO | S_IWUSR, mtktscpu_dir,
  1789. &mtktscpu_Tj_out_fops);
  1790. if (entry)
  1791. proc_set_user(entry, uid, gid);
  1792. #if THERMAL_GPIO_OUT_TOGGLE
  1793. entry =
  1794. proc_create("tzcpu_GPIO_out_monitor", S_IRUGO | S_IWUSR, mtktscpu_dir,
  1795. &mtktscpu_GPIO_out_fops);
  1796. if (entry)
  1797. proc_set_user(entry, uid, gid);
  1798. #endif
  1799. }
  1800. }
  1801. /*must wait until AUXADC initial ready*/
  1802. static int tscpu_thermal_probe(struct platform_device *dev)
  1803. {
  1804. int err = 0;
  1805. tscpu_printk("thermal_prob\n");
  1806. /*
  1807. default is dule mode(irq/reset), if not to config this and hot happen,
  1808. system will reset after 30 secs
  1809. Thermal need to config to direct reset mode
  1810. this API provide by Weiqi Fu(RGU SW owner).
  1811. */
  1812. if (get_io_reg_base() == 0)
  1813. return 0;
  1814. #if !defined(CONFIG_MTK_CLKMGR)
  1815. #if defined(CONFIG_ARCH_MT6755)
  1816. /*Patch to pause thermal controller and turn off auxadc GC.
  1817. For mt6755 only*/
  1818. therm_auxadc = devm_clk_get(&dev->dev, "therm-auxadc");
  1819. if (IS_ERR(therm_auxadc))
  1820. tscpu_printk("[auxadc] cannot get auxadc clock\n");
  1821. tscpu_printk("[AUXADC]: auxadc CLK:0x%p\n", therm_auxadc);
  1822. #endif
  1823. therm_main = devm_clk_get(&dev->dev, "therm-main");
  1824. if (IS_ERR(therm_main)) {
  1825. tscpu_printk("cannot get thermal clock.\n");
  1826. return PTR_ERR(therm_main);
  1827. }
  1828. tscpu_dprintk("therm-main Ptr=%p", therm_main);
  1829. #endif
  1830. tscpu_thermal_clock_on();
  1831. init_thermal();
  1832. #if MTK_TS_CPU_RT
  1833. {
  1834. tscpu_dprintk("tscpu_register_thermal creates kthermp\n");
  1835. ktp_thread_handle = kthread_create(ktp_thread, (void *)NULL, "kthermp");
  1836. if (IS_ERR(ktp_thread_handle)) {
  1837. ktp_thread_handle = NULL;
  1838. tscpu_printk("tscpu_register_thermal kthermp creation fails\n");
  1839. goto err_unreg;
  1840. }
  1841. wake_up_process(ktp_thread_handle);
  1842. }
  1843. #endif
  1844. #ifdef CONFIG_OF
  1845. err =
  1846. request_irq(thermal_irq_number, tscpu_thermal_all_bank_interrupt_handler,
  1847. IRQF_TRIGGER_LOW, THERMAL_NAME, NULL);
  1848. if (err)
  1849. tscpu_warn("tscpu_init IRQ register fail\n");
  1850. #else
  1851. err =
  1852. request_irq(THERM_CTRL_IRQ_BIT_ID, tscpu_thermal_all_bank_interrupt_handler,
  1853. IRQF_TRIGGER_LOW, THERMAL_NAME, NULL);
  1854. if (err)
  1855. tscpu_warn("tscpu_init IRQ register fail\n");
  1856. #endif
  1857. tscpu_config_all_tc_hw_protect(trip_temp[0], tc_mid_trip);
  1858. #if THERMAL_GET_AHB_BUS_CLOCK
  1859. thermal_get_AHB_clk_info();
  1860. #endif
  1861. return err;
  1862. }
  1863. #if defined(CONFIG_ARCH_MT6753)
  1864. /*For MT6753 PMIC 5A throttle patch*/
  1865. int isMT6753T(void)
  1866. {
  1867. unsigned int cpu_spd_bond, efuse_spare2, is53T = 0;
  1868. cpu_spd_bond = (get_devinfo_with_index(3) & _BITMASK_(2:0));
  1869. efuse_spare2 = (get_devinfo_with_index(5) & _BITMASK_(21:20)) >> 20;
  1870. switch (cpu_spd_bond) {
  1871. case 0:
  1872. if (efuse_spare2 == 3)
  1873. is53T = 1;
  1874. break;
  1875. case 1:
  1876. case 2:
  1877. is53T = 1;
  1878. break;
  1879. default:
  1880. break;
  1881. }
  1882. return is53T;
  1883. }
  1884. #endif
  1885. static int __init tscpu_init(void)
  1886. {
  1887. int err = 0;
  1888. tscpu_printk("tscpu_init\n");
  1889. err = platform_driver_register(&mtk_thermal_driver);
  1890. if (err) {
  1891. tscpu_warn("thermal driver callback register failed..\n");
  1892. return err;
  1893. }
  1894. err = tscpu_register_thermal();
  1895. if (err) {
  1896. tscpu_warn("tscpu_register_thermal fail\n");
  1897. goto err_unreg;
  1898. }
  1899. #if defined(CONFIG_ARCH_MT6753)
  1900. /*For MT6753 PMIC 5A throttle patch*/
  1901. if (isMT6753T() == 0)
  1902. fast_polling_trip_temp = 40000;
  1903. #endif
  1904. tscpu_create_fs();
  1905. return 0;
  1906. err_unreg:
  1907. return err;
  1908. }
  1909. static void __exit tscpu_exit(void)
  1910. {
  1911. tscpu_dprintk("tscpu_exit\n");
  1912. #if MTK_TS_CPU_RT
  1913. if (ktp_thread_handle)
  1914. kthread_stop(ktp_thread_handle);
  1915. #endif
  1916. tscpu_unregister_thermal();
  1917. #if THERMAL_DRV_UPDATE_TEMP_DIRECT_TO_MET
  1918. mt_thermalsampler_registerCB(NULL);
  1919. #endif
  1920. }
  1921. module_init(tscpu_init);
  1922. module_exit(tscpu_exit);