mtk_ts_cpu.c 61 KB

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