mtk_thermal_monitor.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524
  1. #ifdef pr_fmt
  2. #undef pr_fmt
  3. #endif
  4. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  5. #include <asm/uaccess.h>
  6. #include <linux/version.h>
  7. #include <linux/kernel.h>
  8. #include <linux/printk.h>
  9. #include <linux/module.h>
  10. #include <linux/dmi.h>
  11. #include <linux/acpi.h>
  12. #include <linux/thermal.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/types.h>
  15. #include <linux/delay.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/err.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/time.h>
  20. #include <linux/string.h>
  21. #include <linux/mutex.h>
  22. #include <linux/bug.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/slab.h>
  25. #include <mt-plat/mtk_thermal_monitor.h>
  26. #include <mt-plat/mtk_thermal_platform.h>
  27. #include <linux/uidgid.h>
  28. /*#ifdef CONFIG_MD32_SUPPORT
  29. #define CONFIG_MTK_THERMAL_EXT_CONTROL
  30. #endif*/
  31. /* ************************************ */
  32. /* Definition */
  33. /* ************************************ */
  34. /**
  35. * \def MTK_THERMAL_MONITOR_MEASURE_GET_TEMP_OVERHEAD
  36. * 1 to enable
  37. * 0 to disable
  38. */
  39. #define MTK_THERMAL_MONITOR_MEASURE_GET_TEMP_OVERHEAD (0)
  40. #define MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS (3)
  41. #define MTK_THERMAL_MONITOR_CONDITIONAL_COOLING (1)
  42. /**
  43. * \def MTK_MAX_STEP_SMA_LEN
  44. * If not defined as 1, multi-step temperature SMA len is supported.
  45. * For example, MTK_MAX_STEP_SMA_LEN is defined as 4.
  46. * Users can set 4 different SMA len for a thermal zone and assign a high threshold for each.
  47. * SMA len in the next step is applied if temp of the TZ reaches high threshold.
  48. * Represent this in a simple figure as below:
  49. * -infinite HT(0)|<- sma_len(0) ->|HT(1)|<- sma_len(1) ->|HT(2)|<- sma_len(2)
  50. * ->|HT(3)|<- sma_len(3)-> |+infinite HT(4)
  51. * In temp range between HT(i) and HT(i+1), sma_len(i) is applied.
  52. * HT(i) < HT(i+1), eq is not allowed since meaningless
  53. * sma_len(i) in [1, 60]
  54. */
  55. #define MAX_STEP_MA_LEN (4)
  56. #define MSMA_MAX_HT (1000000)
  57. #define MSMA_MIN_HT (-275000)
  58. struct mtk_thermal_cooler_data {
  59. struct thermal_zone_device *tz;
  60. struct thermal_cooling_device_ops *ops;
  61. void *devdata;
  62. int trip;
  63. char conditions[MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS][THERMAL_NAME_LENGTH];
  64. int *condition_last_value[MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS];
  65. int threshold[MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS];
  66. int exit_threshold;
  67. int id;
  68. };
  69. struct mtk_thermal_tz_data {
  70. struct thermal_zone_device_ops *ops;
  71. unsigned int ma_len; /* max 60 */
  72. unsigned int ma_counter;
  73. long ma[60];
  74. #if (MAX_STEP_MA_LEN > 1)
  75. unsigned int curr_idx_ma_len;
  76. unsigned int ma_lens[MAX_STEP_MA_LEN];
  77. long msma_ht[MAX_STEP_MA_LEN];
  78. /**< multi-step moving avg. high threshold array. */
  79. #endif
  80. long fake_temp;
  81. /* to store the Tfake, range from -275000 to MAX positive of int...
  82. -275000 is a special number to turn off Tfake */
  83. struct mutex ma_lock; /* protect moving avg. vars... */
  84. };
  85. struct proc_dir_entry *mtk_thermal_get_proc_drv_therm_dir_entry(void);
  86. static DEFINE_MUTEX(MTM_GET_TEMP_LOCK);
  87. static int *tz_last_values[MTK_THERMAL_SENSOR_COUNT] = { NULL };
  88. /* ************************************ */
  89. /* Global Variable */
  90. /* ************************************ */
  91. struct thermal_zone_device_ops *g_SysinfoAttachOps;
  92. static bool enable_ThermalMonitor;
  93. static bool enable_ThermalMonitorXlog;
  94. static int g_nStartRealTime;
  95. static struct proc_dir_entry *proc_cooler_dir_entry; /* lock by MTM_COOLER_PROC_DIR_LOCK */
  96. static struct proc_dir_entry *proc_tz_dir_entry; /* lock by MTK_TZ_PROC_DIR_LOCK */
  97. static struct proc_dir_entry *proc_drv_therm_dir_entry;
  98. /**
  99. * write to nBattCurrentCnsmpt, nCPU0_usage, and nCPU1_usage are locked by MTM_SYSINFO_LOCK
  100. */
  101. static int nBattCurrentCnsmpt;
  102. static int nCPU_loading_sum;
  103. /* 64bit */
  104. static unsigned long long g_check_cpu_info_flag = 0x0;
  105. static unsigned long long g_check_batt_info_flag = 0x0;
  106. static unsigned long long g_check_wifi_info_flag = 0x0;
  107. static unsigned long long g_check_mobile_info_flag = 0x0;
  108. static int nWifi_throughput;
  109. static int nMobile_throughput;
  110. /* static int nModem_TxPower = -127; ///< Indicate invalid value */
  111. /* For enabling time based thermal protection under phone call+AP suspend scenario. */
  112. static int g_mtm_phone_call_ongoing;
  113. static DEFINE_MUTEX(MTM_COOLER_LOCK);
  114. static DEFINE_MUTEX(MTM_SYSINFO_LOCK);
  115. static DEFINE_MUTEX(MTM_COOLER_PROC_DIR_LOCK);
  116. static DEFINE_MUTEX(MTM_TZ_PROC_DIR_LOCK);
  117. static DEFINE_MUTEX(MTM_DRV_THERM_PROC_DIR_LOCK);
  118. static struct delayed_work _mtm_sysinfo_poll_queue;
  119. static kuid_t uid = KUIDT_INIT(0);
  120. static kgid_t gid = KGIDT_INIT(1000);
  121. /* ************************************ */
  122. /* Macro */
  123. /* ************************************ */
  124. #ifdef CONFIG_MTK_MT_LOGGER
  125. #define THRML_STORAGE_LOG(msg_id, func_name, ...) \
  126. do { \
  127. if (unlikely(is_dump_mthermal()) && enable_ThermalMonitor) { \
  128. AddThrmlTrace(msg_id, func_name, __VA_ARGS__); \
  129. } \
  130. } while (0)
  131. #else
  132. #define THRML_STORAGE_LOG(msg_id, func_name, ...)
  133. #endif
  134. #define THRML_LOG(fmt, args...) \
  135. do { \
  136. if (unlikely(enable_ThermalMonitorXlog)) { \
  137. pr_debug("THERMAL/MONITOR " fmt, ##args); \
  138. } \
  139. } while (0)
  140. #define THRML_ERROR_LOG(fmt, args...) pr_debug("THERMAL/MONITOR " fmt, ##args)
  141. /* ************************************ */
  142. /* Define */
  143. /* ************************************ */
  144. /* thermal_zone_device * sysinfo_monitor_register(int nPollingTime); */
  145. /* int sysinfo_monitor_unregister(void); */
  146. #define SYSINFO_ATTACH_DEV_NAME "mtktscpu"
  147. /* ************************************ */
  148. /* Thermal Monitor API */
  149. /* ************************************ */
  150. #if defined(CONFIG_MTK_THERMAL_TIME_BASE_PROTECTION)
  151. #include <mach/mt_gpt.h>
  152. #include <mach/mt_sleep.h>
  153. #include <linux/wakelock.h>
  154. /* extern int force_get_tbat(void); */
  155. static struct wake_lock mtm_wake_lock;
  156. static unsigned int gpt_remaining_cnt;
  157. static int last_batt_raw_temp;
  158. static int mtk_thermal_monitor_get_battery_timeout_time(void)
  159. {
  160. if (NULL != tz_last_values[MTK_THERMAL_SENSOR_BATTERY]) {
  161. int batt_temp = last_batt_raw_temp; /* *tz_last_values[MTK_THERMAL_SENSOR_BATTERY]; */
  162. if (batt_temp <= 25000)
  163. return 330; /* max 330 */
  164. else if (batt_temp <= 35000 && batt_temp > 25000)
  165. return 300;
  166. else if (batt_temp <= 45000 && batt_temp > 35000)
  167. return 150; /* 2.5 min */
  168. else if (batt_temp <= 50000 && batt_temp > 45000)
  169. return 60; /* 1 min */
  170. else
  171. return 30; /* 0.5 min */
  172. } else {
  173. return -1; /* no battery temperature, what to protect? */
  174. }
  175. }
  176. static int mtk_thermal_monitor_suspend(struct platform_device *dev, pm_message_t state)
  177. {
  178. /* check if phone call on going... */
  179. if (g_mtm_phone_call_ongoing) {
  180. /* if yes, based on battery temperature to setup a GPT timer */
  181. int timeout = mtk_thermal_monitor_get_battery_timeout_time();
  182. if (timeout > 0) {
  183. /* restart a one-shot GPT timer // max 5.5 min */
  184. if (gpt_remaining_cnt > 0 && gpt_remaining_cnt <= (timeout * 13000000))
  185. gpt_set_cmp(GPT5, gpt_remaining_cnt);
  186. else
  187. gpt_set_cmp(GPT5, timeout * 13000000); /* compare unit is (1/13M) s */
  188. start_gpt(GPT5);
  189. THRML_ERROR_LOG("%s timeout: %d, gpt_remaining_cnt: %u\n", __func__,
  190. timeout, gpt_remaining_cnt);
  191. }
  192. /* make GPT able to wake up AP */
  193. slp_set_wakesrc(WAKE_SRC_CFG_KEY | WAKE_SRC_GPT, true, false);
  194. } else {
  195. THRML_LOG("%s disable GPT wakes AP.\n", __func__);
  196. /* make GPT unable to wake up AP */
  197. slp_set_wakesrc(WAKE_SRC_CFG_KEY | WAKE_SRC_GPT, false, false);
  198. }
  199. return 0;
  200. }
  201. static int mtk_thermal_monitor_resume(struct platform_device *dev)
  202. {
  203. /* take wake lock */
  204. if (NULL != tz_last_values[MTK_THERMAL_SENSOR_BATTERY]) {
  205. /* check if phone call on going...if yes,
  206. we need to confirm battery temp. if not, we don't need this. */
  207. if (g_mtm_phone_call_ongoing) {
  208. unsigned int GPT5_cmp;
  209. unsigned int GPT5_cnt;
  210. int gpt_counting;
  211. gpt_counting = gpt_is_counting(GPT5);
  212. gpt_get_cmp(GPT5, &GPT5_cmp);
  213. gpt_get_cnt(GPT5, &GPT5_cnt);
  214. gpt_remaining_cnt = GPT5_cmp - GPT5_cnt;
  215. /* If no wake lock taken and gpt does timeout! */
  216. if (!wake_lock_active(&mtm_wake_lock) && !gpt_counting) {
  217. THRML_ERROR_LOG("%s wake_lock() counting=%d, cmp=%u, cnt=%u",
  218. __func__, gpt_counting, GPT5_cmp, GPT5_cnt);
  219. wake_lock(&mtm_wake_lock);
  220. }
  221. }
  222. }
  223. /* cancel my own GPT timer, ok to do it w/o pairing */
  224. stop_gpt(GPT5);
  225. /* release wake lock until no problem... */
  226. return 0;
  227. }
  228. static struct platform_driver mtk_thermal_monitor_driver = {
  229. .remove = NULL,
  230. .shutdown = NULL,
  231. .probe = NULL,
  232. .suspend = mtk_thermal_monitor_suspend,
  233. .resume = mtk_thermal_monitor_resume,
  234. .driver = {
  235. .name = "mtk-therm-mon",
  236. },
  237. };
  238. #endif
  239. #if MTK_THERMAL_MONITOR_MEASURE_GET_TEMP_OVERHEAD
  240. static long int _get_current_time_us(void)
  241. {
  242. struct timeval t;
  243. do_gettimeofday(&t);
  244. return (t.tv_sec & 0xFFF) * 1000000 + t.tv_usec;
  245. }
  246. #endif
  247. #ifdef CONFIG_MTK_THERMAL_EXT_CONTROL
  248. #include "md32_ipi.h"
  249. #include "md32_helper.h"
  250. #include <mach/mtk_thermal_ext_control.h>
  251. #define MTK_THERMAL_DEFAULT_MAX_TEMPERATURE 300000
  252. #define MTK_THERMAL_MAX_TRIP_NUM 20
  253. enum mtk_thermal_control_state {
  254. MTK_THERMAL_CONTROL_STATE_NONE = 0,
  255. MTK_THERMAL_CONTROL_STATE_POLLING,
  256. MTK_THERMAL_CONTROL_STATE_SWITCHING,
  257. MTK_THERMAL_CONTROL_STATE_INTERRUPT,
  258. };
  259. struct mtk_thermal_ext_tz_data {
  260. long high_trip_point;
  261. long low_trip_point;
  262. int polling_delay;
  263. int last_temperature;
  264. int trips;
  265. struct mtk_thermal_tz_data *tzdata;
  266. struct thermal_zone_device *tz;
  267. bool set;
  268. };
  269. static int g_controlState;
  270. static DEFINE_MUTEX(mtk_thermal_ext_control_lock);
  271. static struct mtk_thermal_ext_tz_data mtk_thermal_ext_tz_values[MTK_THERMAL_EXT_SENSOR_COUNT];
  272. static int mtk_thermal_ext_get_threshold(struct mtk_thermal_ext_tz_data *tzdata,
  273. struct thermal_zone_device *thermal, struct thermal_zone_device_ops *ops, int trips) {
  274. unsigned long temperature;
  275. int i, j, ret, trip_num;
  276. long trip_point[MTK_THERMAL_MAX_TRIP_NUM] = { 0 };
  277. long temp;
  278. if (!tzdata || !thermal || !ops) {
  279. THRML_ERROR_LOG("%s invalid parameter\n", __func__);
  280. return -1;
  281. }
  282. trip_num = (trips < MTK_THERMAL_MAX_TRIP_NUM) ? trips : MTK_THERMAL_MAX_TRIP_NUM;
  283. if (ops->get_trip_temp) {
  284. for (i = 0; i < trip_num; i++) {
  285. ret = ops->get_trip_temp(thermal, i, &temperature);
  286. trip_point[i] = (long)temperature;
  287. }
  288. if (trip_num > 1) {
  289. /* Sort trip point */
  290. for (i = (trip_num - 1); i > 0; i--) {
  291. for (j = 0; j < i; j++) {
  292. if (trip_point[j] > trip_point[j + 1]) {
  293. temp = trip_point[j];
  294. trip_point[j] = trip_point[j + 1];
  295. trip_point[j + 1] = temp;
  296. }
  297. }
  298. }
  299. /*
  300. * Get trip points
  301. * Condition 1. temperature < low_trip_point < high_trip_point
  302. * Condition 2. low_trip_point <= temperature < high_trip_point
  303. */
  304. for (i = 0; i < trip_num; i++) {
  305. if (tzdata->last_temperature < trip_point[i]) {
  306. if (i == 0) {
  307. tzdata->low_trip_point = trip_point[0];
  308. tzdata->high_trip_point = trip_point[1];
  309. } else {
  310. tzdata->low_trip_point = trip_point[i - 1];
  311. tzdata->high_trip_point = trip_point[i];
  312. }
  313. break;
  314. } else if (i == (trip_num - 1)) {
  315. tzdata->low_trip_point = trip_point[i];
  316. tzdata->high_trip_point =
  317. MTK_THERMAL_DEFAULT_MAX_TEMPERATURE;
  318. }
  319. }
  320. } else if (trip_num == 1) {
  321. tzdata->low_trip_point = trip_point[0];
  322. tzdata->high_trip_point = MTK_THERMAL_DEFAULT_MAX_TEMPERATURE;
  323. } else {
  324. tzdata->low_trip_point = 0;
  325. tzdata->high_trip_point = 0;
  326. }
  327. } else {
  328. return -1;
  329. }
  330. return 0;
  331. }
  332. static int mtk_thermal_ext_get_temp(struct thermal_zone_device *tz, int *temp)
  333. {
  334. int i;
  335. mutex_lock(&mtk_thermal_ext_control_lock);
  336. if (g_controlState == MTK_THERMAL_CONTROL_STATE_INTERRUPT) {
  337. for (i = 0; i < MTK_THERMAL_EXT_SENSOR_COUNT; i++) {
  338. if (mtk_thermal_ext_tz_values[i].set
  339. && mtk_thermal_ext_tz_values[i].tz == tz) {
  340. *temp = mtk_thermal_ext_tz_values[i].last_temperature;
  341. THRML_LOG("%s tz %s, return temp %d\n", __func__, tz->type, *temp);
  342. mutex_unlock(&mtk_thermal_ext_control_lock);
  343. return 0;
  344. }
  345. }
  346. }
  347. mutex_unlock(&mtk_thermal_ext_control_lock);
  348. return -1;
  349. }
  350. static bool mtk_thermal_ext_ipi_msg_send(thermal_ipi_msg_id id, thermal_zone_data *tzdata,
  351. uint wait)
  352. {
  353. thermal_ipi_msg msg;
  354. ipi_status status;
  355. THRML_LOG("%s msg id %x\n", __func__, (unsigned int)id);
  356. memset(&msg, 0, sizeof(thermal_ipi_msg));
  357. msg.id = id;
  358. if (tzdata != NULL) {
  359. msg.data.tz.id = tzdata->id;
  360. msg.data.tz.high_trip_point = tzdata->high_trip_point;
  361. msg.data.tz.low_trip_point = tzdata->low_trip_point;
  362. msg.data.tz.polling_delay = tzdata->polling_delay;
  363. }
  364. status = md32_ipi_send(IPI_THERMAL, (void *)&msg, sizeof(thermal_ipi_msg), wait);
  365. if (status != DONE) {
  366. THRML_ERROR_LOG("%s send fail, ret %d\n", __func__, status);
  367. return false;
  368. }
  369. return true;
  370. }
  371. static void mtk_thermal_ext_set_tz_threshold(struct mtk_thermal_ext_tz_data *tzdata, int idx)
  372. {
  373. thermal_zone_data tz_threshold;
  374. tz_threshold.id = idx;
  375. tz_threshold.high_trip_point = tzdata->high_trip_point;
  376. tz_threshold.low_trip_point = tzdata->low_trip_point;
  377. if (tzdata->trips > 0)
  378. tz_threshold.polling_delay = tzdata->polling_delay;
  379. else
  380. tz_threshold.polling_delay = 0;
  381. THRML_LOG("%s id: %d, polling delay: %d, low trip: %d, high trip: %d\n", __func__,
  382. idx,
  383. tz_threshold.polling_delay,
  384. tz_threshold.low_trip_point, tz_threshold.high_trip_point);
  385. mtk_thermal_ext_ipi_msg_send(THERMAL_AP_IPI_MSG_SET_TZ_THRESHOLD, &tz_threshold, true);
  386. }
  387. static void mtk_thermal_ext_update_tz_threshold(int tzidx, int temperature)
  388. {
  389. int result;
  390. mutex_lock(&mtk_thermal_ext_control_lock);
  391. mtk_thermal_ext_tz_values[tzidx].last_temperature = temperature;
  392. result = mtk_thermal_ext_get_threshold(&mtk_thermal_ext_tz_values[tzidx],
  393. mtk_thermal_ext_tz_values[tzidx].tz,
  394. mtk_thermal_ext_tz_values[tzidx].tzdata->ops,
  395. mtk_thermal_ext_tz_values[tzidx].trips);
  396. if (result < 0) {
  397. mtk_thermal_ext_tz_values[tzidx].high_trip_point =
  398. MTK_THERMAL_DEFAULT_MAX_TEMPERATURE;
  399. mtk_thermal_ext_tz_values[tzidx].low_trip_point =
  400. MTK_THERMAL_DEFAULT_MAX_TEMPERATURE;
  401. }
  402. mutex_unlock(&mtk_thermal_ext_control_lock);
  403. mtk_thermal_ext_set_tz_threshold(&mtk_thermal_ext_tz_values[tzidx], tzidx);
  404. }
  405. static void mtk_thermal_ext_switch_control_back(void)
  406. {
  407. int i;
  408. /* Switch state from interrupt mode to polling mode */
  409. mutex_lock(&mtk_thermal_ext_control_lock);
  410. if (g_controlState == MTK_THERMAL_CONTROL_STATE_INTERRUPT) {
  411. for (i = 0; i < MTK_THERMAL_EXT_SENSOR_COUNT; i++) {
  412. if (mtk_thermal_ext_tz_values[i].set
  413. && mtk_thermal_ext_tz_values[i].polling_delay > 0) {
  414. schedule_delayed_work(&
  415. (mtk_thermal_ext_tz_values[i].tz->poll_queue),
  416. 0);
  417. }
  418. }
  419. g_controlState = MTK_THERMAL_CONTROL_STATE_POLLING;
  420. }
  421. mutex_unlock(&mtk_thermal_ext_control_lock);
  422. }
  423. static void mtk_thermal_ext_switch_control_out(void)
  424. {
  425. int i;
  426. /* [Warning] Not lock here because md32_ipi_send() also lock and
  427. * it will result in kernel warning (LockProve Warning) */
  428. /* mutex_lock(&mtk_thermal_ext_control_lock); */
  429. if (g_controlState == MTK_THERMAL_CONTROL_STATE_POLLING) {
  430. for (i = 0; i < MTK_THERMAL_EXT_SENSOR_COUNT; i++) {
  431. if (mtk_thermal_ext_tz_values[i].set)
  432. mtk_thermal_ext_update_tz_threshold(i, *tz_last_values[i]);
  433. }
  434. g_controlState = MTK_THERMAL_CONTROL_STATE_SWITCHING;
  435. }
  436. /* mutex_unlock(&mtk_thermal_ext_control_lock); */
  437. mtk_thermal_ext_ipi_msg_send(THERMAL_AP_IPI_MSG_MD32_START, NULL, false);
  438. }
  439. static void mtk_thermal_ext_ipi_msg_handler(int id, void *data, uint len)
  440. {
  441. thermal_ipi_msg *msg = (thermal_ipi_msg *) data;
  442. int i;
  443. THRML_LOG("%s id %d, msg id %x, len %d\n", __func__, id, msg->id, len);
  444. switch (msg->id) {
  445. case THERMAL_MD32_IPI_MSG_READY:
  446. {
  447. mtk_thermal_ext_switch_control_out();
  448. break;
  449. }
  450. case THERMAL_MD32_IPI_MSG_MD32_START_ACK:
  451. {
  452. mutex_lock(&mtk_thermal_ext_control_lock);
  453. if (g_controlState != MTK_THERMAL_CONTROL_STATE_SWITCHING)
  454. break;
  455. for (i = 0; i < MTK_THERMAL_EXT_SENSOR_COUNT; i++) {
  456. if (mtk_thermal_ext_tz_values[i].set) {
  457. /* [Warning] Can not use cancel_delayed_work_sync() here
  458. * because it will cause kernel warning (LockProve Warning) */
  459. if (cancel_delayed_work(&(mtk_thermal_ext_tz_values[i].tz->poll_queue)) == 0)
  460. THRML_ERROR_LOG("%s work (tz %d) is running\n", __func__, i);
  461. }
  462. }
  463. g_controlState = MTK_THERMAL_CONTROL_STATE_INTERRUPT;
  464. mutex_unlock(&mtk_thermal_ext_control_lock);
  465. break;
  466. }
  467. case THERMAL_MD32_IPI_MSG_REACH_THRESHOLD:
  468. {
  469. int tzidx = msg->data.tz_status.id;
  470. struct thermal_zone_device *tz = NULL;
  471. /* [Warning] Not lock here because md32_ipi_send() also lock and
  472. * it will result in kernel warning (LockProve Warning) */
  473. /* mutex_lock(&mtk_thermal_ext_control_lock); */
  474. if (g_controlState == MTK_THERMAL_CONTROL_STATE_INTERRUPT) {
  475. if (mtk_thermal_ext_tz_values[tzidx].set) {
  476. tz = mtk_thermal_ext_tz_values[tzidx].tz;
  477. mtk_thermal_ext_update_tz_threshold(tzidx,
  478. (int)msg->
  479. data.tz_status.
  480. temperature);
  481. }
  482. }
  483. /* mutex_unlock(&mtk_thermal_ext_control_lock); */
  484. if (tz != NULL) {
  485. thermal_zone_device_update(tz);
  486. /* [Warning] Can not use cancel_delayed_work_sync() here
  487. because it will cause kernel warning (LockProve Warning) */
  488. if (cancel_delayed_work(&(tz->poll_queue)) == 0) {
  489. THRML_ERROR_LOG("%s work (tz %d) is running\n", __func__,
  490. tzidx);
  491. }
  492. }
  493. break;
  494. }
  495. }
  496. }
  497. static int mtk_thermal_ext_proc_show(struct seq_file *m, void *v)
  498. {
  499. int i;
  500. mutex_lock(&mtk_thermal_ext_control_lock);
  501. seq_puts(m, "\r\n[EXT Thermal Control Debug]\r\n");
  502. seq_puts(m, "=========================================\r\n");
  503. seq_printf(m, "ap thermal state = %d\r\n", g_controlState);
  504. for (i = 0; i < MTK_THERMAL_EXT_SENSOR_COUNT; i++) {
  505. seq_printf(m,
  506. "tz %s, id: %d, set: %d, temp: %d, polling delay: %d, low trip: %d, high trip: %d\r\n",
  507. mtk_thermal_ext_tz_values[i].tz->type, i,
  508. mtk_thermal_ext_tz_values[i].set,
  509. mtk_thermal_ext_tz_values[i].last_temperature,
  510. mtk_thermal_ext_tz_values[i].polling_delay,
  511. mtk_thermal_ext_tz_values[i].low_trip_point,
  512. mtk_thermal_ext_tz_values[i].high_trip_point);
  513. }
  514. mutex_unlock(&mtk_thermal_ext_control_lock);
  515. return 0;
  516. }
  517. static int mtk_thermal_ext_proc_open(struct inode *inode, struct file *file)
  518. {
  519. return single_open(file, mtk_thermal_ext_proc_show, NULL);
  520. }
  521. static const struct file_operations mtk_thermal_ext_proc_fops = {
  522. .owner = THIS_MODULE,
  523. .open = mtk_thermal_ext_proc_open,
  524. .read = seq_read,
  525. .llseek = seq_lseek,
  526. .release = single_release,
  527. };
  528. static int mtk_thermal_ext_get_tz_idx(char *type)
  529. {
  530. if (0 == strncmp(type, "mtktsabb", 8))
  531. return MTK_THERMAL_EXT_SENSOR_ABB;
  532. else if (0 == strncmp(type, "mtktspmic", 9))
  533. return MTK_THERMAL_EXT_SENSOR_PMIC;
  534. else if (0 == strncmp(type, "mtktsbattery2", 13))
  535. return -1;
  536. else if (0 == strncmp(type, "mtktsbattery", 12))
  537. return MTK_THERMAL_EXT_SENSOR_BATTERY;
  538. return -1;
  539. }
  540. static int mtk_thermal_ext_notify(struct notifier_block *self, unsigned long action, void *dev)
  541. {
  542. #ifdef DYNAMIC_TCM_SWAP
  543. MD32_REQUEST_SWAP *request_swap = (MD32_REQUEST_SWAP *) dev;
  544. THRML_LOG("%s action: %d, current group: %d, start group: %d\n", __func__,
  545. action, request_swap->current_group, request_swap->group_start);
  546. switch (action) {
  547. case APP_TRIGGER_TCM_SWAP_START:
  548. {
  549. if (request_swap->prepare_result < 0) {
  550. /* MD32 dynamic swap prepare failed */
  551. break;
  552. }
  553. if (request_swap->current_group == GROUP_BASIC
  554. && request_swap->group_start == GROUP_A) {
  555. mtk_thermal_ext_switch_control_back();
  556. }
  557. break;
  558. }
  559. case APP_TRIGGER_TCM_SWAP_DONE:
  560. {
  561. if (request_swap->current_group == GROUP_A
  562. && request_swap->group_start == GROUP_BASIC) {
  563. mtk_thermal_ext_switch_control_out();
  564. }
  565. break;
  566. }
  567. case APP_TRIGGER_TCM_SWAP_FAIL:
  568. {
  569. if (request_swap->current_group == GROUP_BASIC
  570. && request_swap->group_start == GROUP_A) {
  571. mtk_thermal_ext_switch_control_out();
  572. }
  573. break;
  574. }
  575. case APP_TRIGGER_APP_FINISHED:
  576. break;
  577. default:
  578. break;
  579. }
  580. #endif
  581. return NOTIFY_OK;
  582. }
  583. static struct notifier_block mtk_thermal_ext_nb = {
  584. .notifier_call = mtk_thermal_ext_notify,
  585. };
  586. #endif /* CONFIG_MTK_THERMAL_EXT_CONTROL */
  587. static int mtk_thermal_get_tz_idx(char *type)
  588. {
  589. if (0 == strncmp(type, "mtktscpu", 8))
  590. return MTK_THERMAL_SENSOR_CPU;
  591. else if (0 == strncmp(type, "mtktsabb", 8))
  592. return MTK_THERMAL_SENSOR_ABB;
  593. else if (0 == strncmp(type, "mtktspmic", 9))
  594. return MTK_THERMAL_SENSOR_PMIC;
  595. else if (0 == strncmp(type, "mtktsbattery2", 13))
  596. return MTK_THERMAL_SENSOR_BATTERY2;
  597. else if (0 == strncmp(type, "mtktsbattery", 12))
  598. return MTK_THERMAL_SENSOR_BATTERY;
  599. else if (0 == strncmp(type, "mtktspa", 7))
  600. return MTK_THERMAL_SENSOR_MD1;
  601. else if (0 == strncmp(type, "mtktstdpa", 9))
  602. return MTK_THERMAL_SENSOR_MD2;
  603. else if (0 == strncmp(type, "mtktswmt", 8))
  604. return MTK_THERMAL_SENSOR_WIFI;
  605. else if (0 == strncmp(type, "mtktsbuck", 9))
  606. return MTK_THERMAL_SENSOR_BUCK;
  607. else if (0 == strncmp(type, "mtktsAP", 7))
  608. return MTK_THERMAL_SENSOR_AP;
  609. else if (0 == strncmp(type, "mtktspcb1", 9))
  610. return MTK_THERMAL_SENSOR_PCB1;
  611. else if (0 == strncmp(type, "mtktspcb2", 9))
  612. return MTK_THERMAL_SENSOR_PCB2;
  613. else if (0 == strncmp(type, "mtktsskin", 9))
  614. return MTK_THERMAL_SENSOR_SKIN;
  615. else if (0 == strncmp(type, "mtktsxtal", 9))
  616. return MTK_THERMAL_SENSOR_XTAL;
  617. return -1;
  618. }
  619. static struct proc_dir_entry *_get_proc_cooler_dir_entry(void)
  620. {
  621. mutex_lock(&MTM_COOLER_PROC_DIR_LOCK);
  622. if (NULL == proc_cooler_dir_entry) {
  623. proc_cooler_dir_entry = proc_mkdir("mtkcooler", NULL);
  624. mb();
  625. if (NULL == proc_cooler_dir_entry)
  626. THRML_ERROR_LOG("%s mkdir /proc/mtkcooler failed\n", __func__);
  627. }
  628. mutex_unlock(&MTM_COOLER_PROC_DIR_LOCK);
  629. return proc_cooler_dir_entry;
  630. }
  631. static struct proc_dir_entry *_get_proc_tz_dir_entry(void)
  632. {
  633. mutex_lock(&MTM_TZ_PROC_DIR_LOCK);
  634. if (NULL == proc_tz_dir_entry) {
  635. proc_tz_dir_entry = proc_mkdir("mtktz", NULL);
  636. mb();
  637. if (NULL == proc_tz_dir_entry)
  638. THRML_ERROR_LOG("%s mkdir /proc/mtktz failed\n", __func__);
  639. }
  640. mutex_unlock(&MTM_TZ_PROC_DIR_LOCK);
  641. return proc_tz_dir_entry;
  642. }
  643. static struct thermal_cooling_device_ops *recoveryClientCooler
  644. (struct thermal_cooling_device *cdev, struct mtk_thermal_cooler_data **mcdata) {
  645. *mcdata = cdev->devdata;
  646. cdev->devdata = (*mcdata)->devdata;
  647. return (*mcdata)->ops;
  648. }
  649. /* Lookup List to get Client's Thermal Zone OPS */
  650. static struct thermal_zone_device_ops *getClientZoneOps(struct thermal_zone_device *zdev)
  651. {
  652. struct thermal_zone_device_ops *ret = NULL;
  653. struct mtk_thermal_tz_data *tzdata;
  654. if ((NULL == zdev) || (NULL == zdev->devdata)) {
  655. BUG();
  656. return NULL;
  657. }
  658. tzdata = zdev->devdata;
  659. mutex_lock(&tzdata->ma_lock);
  660. ret = tzdata->ops;
  661. mutex_unlock(&tzdata->ma_lock);
  662. return ret;
  663. }
  664. #define CPU_USAGE_CURRENT_FIELD (0)
  665. #define CPU_USAGE_SAVE_FIELD (1)
  666. #define CPU_USAGE_FRAME_FIELD (2)
  667. struct cpu_index_st {
  668. unsigned long u[3];
  669. unsigned long s[3];
  670. unsigned long n[3];
  671. unsigned long i[3];
  672. unsigned long w[3];
  673. unsigned long q[3];
  674. unsigned long sq[3];
  675. unsigned long tot_frme;
  676. unsigned long tz;
  677. int usage;
  678. int freq;
  679. };
  680. struct gpu_index_st {
  681. int usage;
  682. int freq;
  683. };
  684. static struct cpu_index_st cpu_index_list[8]; /* /< 8-Core is maximum */
  685. static struct gpu_index_st gpu_index;
  686. #define SEEK_BUFF(x, c) \
  687. do { \
  688. while (*x != c)\
  689. x++; \
  690. x++; \
  691. } while (0)
  692. #define TRIMz_ex(tz, x) ((tz = (unsigned long long)(x)) < 0 ? 0 : tz)
  693. enum {
  694. THERMAL_SYS_INFO_CPU = 0x1,
  695. THERMAL_SYS_INFO_GPU = 0x2,
  696. THERMAL_SYS_INFO_BATT = 0x4,
  697. THERMAL_SYS_INFO_WIFI = 0x8,
  698. THERMAL_SYS_INFO_MD = 0x10,
  699. THERMAL_SYS_INFO_ALL = 0xFFFFFFF
  700. };
  701. static int mtk_sysinfo_get_info(unsigned int mask)
  702. {
  703. int nBattVol, nBattTemp;
  704. int i;
  705. int nocpucores = 0, *cpufreqs, *cpuloadings;
  706. int nogpucores = 0, *gpufreqs, *gpuloadings;
  707. int noextraattr = 0, *attrvalues;
  708. char **attrnames, **attrunits;
  709. if (mask == 0x0)
  710. return 0;
  711. mutex_lock(&MTM_SYSINFO_LOCK);
  712. /* ****************** */
  713. /* Battery */
  714. /* ****************** */
  715. if (mask & THERMAL_SYS_INFO_BATT) {
  716. if (mtk_thermal_get_batt_info(&nBattVol, &nBattCurrentCnsmpt, &nBattTemp))
  717. ; /* TODO: print error log */
  718. }
  719. /* ****************** */
  720. /* CPU Usage */
  721. /* ****************** */
  722. /* ****************** */
  723. /* CPU Frequency */
  724. /* ****************** */
  725. if (mask & THERMAL_SYS_INFO_CPU) {
  726. if (mtk_thermal_get_cpu_info(&nocpucores, &cpufreqs, &cpuloadings))
  727. ; /* TODO: print error log */
  728. else {
  729. for (i = 0; i < nocpucores; i++) {
  730. cpu_index_list[i].freq = cpufreqs[i];
  731. cpu_index_list[i].usage = cpuloadings[i];
  732. }
  733. }
  734. /* CPU loading average */
  735. nCPU_loading_sum = 0;
  736. for (i = 0; i < nocpucores; i++)
  737. nCPU_loading_sum += cpuloadings[i];
  738. }
  739. /* ****************** */
  740. /* GPU Index */
  741. /* ****************** */
  742. if (mask & THERMAL_SYS_INFO_GPU) {
  743. if (mtk_thermal_get_gpu_info(&nogpucores, &gpufreqs, &gpuloadings))
  744. ; /* TODO: print error log */
  745. else {
  746. gpu_index.freq = gpufreqs[0];
  747. gpu_index.usage = gpuloadings[0];
  748. }
  749. }
  750. /* ****************** */
  751. /* Modem Index */
  752. /* ****************** */
  753. /* ****************** */
  754. /* Wifi Index */
  755. /* ****************** */
  756. if (mask & (THERMAL_SYS_INFO_WIFI | THERMAL_SYS_INFO_MD)) {
  757. if (mtk_thermal_get_extra_info(&noextraattr, &attrnames, &attrvalues, &attrunits))
  758. /* TODO: print error log */;
  759. else {
  760. /* THRML_LOG("%s %d, %d, %d, %d, %d, %d, %d, %d\n",
  761. __func__,*(attrvalues +0), *(attrvalues +1), *(attrvalues +2),
  762. *(attrvalues +3), *(attrvalues +4), *(attrvalues +5),
  763. *(attrvalues +6), *(attrvalues +7)); */
  764. nMobile_throughput = *(attrvalues + 7);
  765. THRML_LOG("%s Mobile_throughput=%d\n", __func__, nMobile_throughput);
  766. }
  767. }
  768. mutex_unlock(&MTM_SYSINFO_LOCK);
  769. /* print extra info */
  770. for (i = 0; i < noextraattr; i++) {
  771. THRML_STORAGE_LOG(THRML_LOGGER_MSG_MISC_EX_INFO, get_misc_ex_info, attrnames[i],
  772. attrvalues[i], attrunits[i]);
  773. }
  774. /* print batt info */
  775. if (mask & THERMAL_SYS_INFO_BATT) {
  776. THRML_LOG("%s nBattCurrentCnsmpt=%d nBattVol=%d nBattTemp=%d\n", __func__,
  777. nBattCurrentCnsmpt, nBattVol, nBattTemp);
  778. }
  779. THRML_STORAGE_LOG(THRML_LOGGER_MSG_BATTERY_INFO, get_battery_info, nBattCurrentCnsmpt,
  780. nBattVol, nBattTemp);
  781. /* CPU and GPU to storage logger */
  782. THRML_STORAGE_LOG(THRML_LOGGER_MSG_CPU_INFO_EX, get_cpu_info_ex,
  783. cpu_index_list[0].usage, cpu_index_list[1].usage,
  784. cpu_index_list[2].usage, cpu_index_list[3].usage,
  785. cpu_index_list[0].freq, cpu_index_list[1].freq,
  786. cpu_index_list[2].freq, cpu_index_list[3].freq,
  787. gpu_index.usage, gpu_index.freq);
  788. if (mask & THERMAL_SYS_INFO_CPU) {
  789. THRML_LOG("%s CPU U C0=%d C1=%d C2=%d C3=%d\n", __func__,
  790. cpu_index_list[0].usage, cpu_index_list[1].usage,
  791. cpu_index_list[2].usage, cpu_index_list[3].usage);
  792. THRML_LOG("%s CPU Freq C0=%d C1=%d C2=%d C3=%d\n", __func__,
  793. cpu_index_list[0].freq, cpu_index_list[1].freq,
  794. cpu_index_list[2].freq, cpu_index_list[3].freq);
  795. }
  796. return 0;
  797. }
  798. static int _mtm_interval;
  799. static void _mtm_update_sysinfo(struct work_struct *work)
  800. {
  801. if (true == enable_ThermalMonitor)
  802. mtk_sysinfo_get_info(THERMAL_SYS_INFO_ALL);
  803. else {
  804. unsigned int mask = 0;
  805. mask |= (g_check_cpu_info_flag == 0ULL) ? 0 : THERMAL_SYS_INFO_CPU;
  806. mask |= (g_check_batt_info_flag == 0ULL) ? 0 : THERMAL_SYS_INFO_BATT;
  807. mask |= (g_check_wifi_info_flag == 0ULL) ? 0 : THERMAL_SYS_INFO_WIFI;
  808. mask |= (g_check_mobile_info_flag == 0ULL) ? 0 : THERMAL_SYS_INFO_MD;
  809. mtk_sysinfo_get_info(mask);
  810. }
  811. cancel_delayed_work(&_mtm_sysinfo_poll_queue);
  812. if (_mtm_interval != 0)
  813. queue_delayed_work(system_freezable_wq, &_mtm_sysinfo_poll_queue,
  814. msecs_to_jiffies(_mtm_interval));
  815. }
  816. static void _mtm_decide_new_delay(void)
  817. {
  818. int new_interval = 0;
  819. if (true == enable_ThermalMonitor) {
  820. new_interval = 1000;
  821. } else {
  822. unsigned int mask = 0;
  823. mask |= (g_check_cpu_info_flag == 0ULL) ? 0 : THERMAL_SYS_INFO_CPU;
  824. mask |= (g_check_batt_info_flag == 0ULL) ? 0 : THERMAL_SYS_INFO_BATT;
  825. mask |= (g_check_wifi_info_flag == 0ULL) ? 0 : THERMAL_SYS_INFO_WIFI;
  826. mask |= (g_check_mobile_info_flag == 0ULL) ? 0 : THERMAL_SYS_INFO_MD;
  827. if (mask != 0x0)
  828. new_interval = 1000;
  829. }
  830. if (_mtm_interval == 0 && new_interval != 0) {
  831. _mtm_interval = new_interval;
  832. _mtm_update_sysinfo(NULL);
  833. } else {
  834. _mtm_interval = new_interval;
  835. }
  836. }
  837. /* ************************************ */
  838. /* Thermal Host Driver Interface */
  839. /* ************************************ */
  840. /* Read */
  841. static int mtkthermal_read(struct seq_file *m, void *v)
  842. {
  843. seq_puts(m, "\r\n[Thermal Monitor debug flag]\r\n");
  844. seq_puts(m, "=========================================\r\n");
  845. seq_printf(m, "enable_ThermalMonitor = %d\r\n", enable_ThermalMonitor);
  846. seq_printf(m, "enable_ThermalMonitorXlog = %d\r\n", enable_ThermalMonitorXlog);
  847. seq_printf(m, "g_nStartRealTime = %d\r\n", g_nStartRealTime);
  848. THRML_LOG("%s enable_ThermalMonitor:%d\n", __func__, enable_ThermalMonitor);
  849. return 0;
  850. }
  851. /* Write */
  852. static ssize_t mtkthermal_write(struct file *file, const char __user *buffer, size_t count,
  853. loff_t *data)
  854. {
  855. int len = 0, nCtrlCmd = 0, nReadTime = 0;
  856. char desc[32];
  857. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  858. if (copy_from_user(desc, buffer, len))
  859. return 0;
  860. desc[len] = '\0';
  861. if (sscanf(desc, "%d %d", &nCtrlCmd, &nReadTime) == 2) {
  862. /* Bit 0; Enable Thermal Monitor. */
  863. if ((nCtrlCmd >> 0) & 0x01) {
  864. /* Reset Global CPU Info Variable */
  865. memset(&cpu_index_list, 0x00, sizeof(cpu_index_list));
  866. enable_ThermalMonitor = true;
  867. } else {
  868. enable_ThermalMonitor = false;
  869. }
  870. _mtm_decide_new_delay();
  871. /* Bit 1: Enable Thermal Monitor xlog */
  872. enable_ThermalMonitorXlog = ((nCtrlCmd >> 1) & 0x01) ? true : false;
  873. /*
  874. * Get Real Time from user input
  875. * Format: hhmmss 113901=> 11:39:01
  876. */
  877. g_nStartRealTime = nReadTime;
  878. THRML_STORAGE_LOG(THRML_LOGGER_MSG_DEC_NUM, get_real_time, "[realtime]",
  879. g_nStartRealTime);
  880. THRML_ERROR_LOG("%s nCtrlCmd=%d enable_ThermalMonitor=%d g_nStartRealTime=%d\n",
  881. __func__, nCtrlCmd, (int)enable_ThermalMonitor, g_nStartRealTime);
  882. return count;
  883. }
  884. if (kstrtoint(desc, 10, &nCtrlCmd) == 0) {
  885. /* Bit 0; Enable Thermal Monitor. */
  886. if ((nCtrlCmd >> 0) & 0x01) {
  887. /* Reset Global CPU Info Variable */
  888. memset(&cpu_index_list, 0x00, sizeof(cpu_index_list));
  889. enable_ThermalMonitor = true;
  890. } else {
  891. enable_ThermalMonitor = false;
  892. }
  893. _mtm_decide_new_delay();
  894. /* Bit 1: Enable Thermal Monitor xlog */
  895. enable_ThermalMonitorXlog = ((nCtrlCmd >> 1) & 0x01) ? true : false;
  896. THRML_ERROR_LOG("%s nCtrlCmd=%d enable_ThermalMonitor=%d\n", __func__, nCtrlCmd,
  897. (int)enable_ThermalMonitor);
  898. return count;
  899. }
  900. THRML_LOG("%s bad arg\n", __func__);
  901. return -EINVAL;
  902. }
  903. static int mtkthermal_open(struct inode *inode, struct file *file)
  904. {
  905. return single_open(file, mtkthermal_read, NULL);
  906. }
  907. static const struct file_operations mtkthermal_fops = {
  908. .owner = THIS_MODULE,
  909. .open = mtkthermal_open,
  910. .read = seq_read,
  911. .llseek = seq_lseek,
  912. .write = mtkthermal_write,
  913. .release = single_release,
  914. };
  915. static int _mtkthermal_check_cooler_conditions(struct mtk_thermal_cooler_data *cldata)
  916. {
  917. int ret = 0;
  918. if (NULL != cldata) {
  919. int i = 0;
  920. for (; i < MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS; i++) {
  921. if (NULL == cldata->condition_last_value[i]) {
  922. ret++;
  923. } else {
  924. #if 1 /* [FIX ME] Special case for "condifion of MOBILE" */
  925. if (0 == strncmp(cldata->conditions[i], "MOBILE", 5)) {
  926. if (*cldata->condition_last_value[i] < cldata->threshold[i]) {
  927. THRML_LOG("%s MOBILE Condition=%s, last_value=%d, threshold=%d\n",
  928. __func__, cldata->conditions[i],
  929. (*cldata->condition_last_value[i]), cldata->threshold[i]);
  930. ret++;
  931. }
  932. } else if (*cldata->condition_last_value[i] > cldata->threshold[i])
  933. ret++;
  934. #else
  935. if (*cldata->condition_last_value[i] > cldata->threshold[i])
  936. ret++;
  937. #endif
  938. }
  939. }
  940. mb();
  941. }
  942. return ret;
  943. }
  944. static void _mtkthermal_clear_cooler_conditions(struct mtk_thermal_cooler_data *cldata)
  945. {
  946. int i = 0;
  947. cldata->exit_threshold = 0;
  948. for (; i < MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS; i++) {
  949. cldata->conditions[i][0] = 0x0;
  950. cldata->condition_last_value[i] = NULL;
  951. cldata->threshold[i] = 0;
  952. }
  953. g_check_cpu_info_flag &= (~(1ULL << cldata->id));
  954. g_check_batt_info_flag &= (~(1ULL << cldata->id));
  955. g_check_wifi_info_flag &= (~(1ULL << cldata->id));
  956. g_check_mobile_info_flag &= (~(1ULL << cldata->id));
  957. _mtm_decide_new_delay();
  958. }
  959. static int _mtkthermal_cooler_read(struct seq_file *m, void *v)
  960. {
  961. struct mtk_thermal_cooler_data *mcdata;
  962. /**
  963. * The format to print out
  964. * <condition_name_1> <condition_value_1> <thershold_1> <state_1>
  965. * ..
  966. * <condition_name_n> <condition_value_n> <thershold_n> <state_n>
  967. * PS: n is MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS
  968. */
  969. if (NULL == m->private) {
  970. THRML_ERROR_LOG("%s null data\n", __func__);
  971. } else {
  972. int i = 0;
  973. /* TODO: we may not need to lock here... */
  974. mutex_lock(&MTM_COOLER_LOCK);
  975. mcdata = (struct mtk_thermal_cooler_data *)m->private;
  976. mutex_unlock(&MTM_COOLER_LOCK);
  977. for (; i < MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS; i++) {
  978. if (0x0 == mcdata->conditions[i][0])
  979. continue; /* no condition */
  980. /* TODO: consider the case that tz is unregistered... */
  981. seq_printf(m, "%s val=%d threshold=%d %s",
  982. mcdata->conditions[i],
  983. (NULL ==
  984. mcdata->
  985. condition_last_value[i]) ? 0 :
  986. *(mcdata->condition_last_value[i]), mcdata->threshold[i],
  987. (NULL == mcdata->condition_last_value[i]) ? "error\n" : "\n");
  988. }
  989. }
  990. return 0;
  991. }
  992. static ssize_t _mtkthermal_cooler_write(struct file *file, const char __user *buffer, size_t count, loff_t *data)
  993. {
  994. int len = 0;
  995. char desc[128];
  996. struct mtk_thermal_cooler_data *mcdata;
  997. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  998. if (copy_from_user(desc, buffer, len))
  999. return 0;
  1000. desc[len] = '\0';
  1001. /**
  1002. * sscanf format <condition_1> <threshold_1> ... <condition_n> <threshold_n>
  1003. * <condition_i> is string format
  1004. * <threshold_i> is integer format
  1005. * n is MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS
  1006. */
  1007. /* TODO: we may not need to lock here... */
  1008. mutex_lock(&MTM_COOLER_LOCK);
  1009. mcdata = (struct mtk_thermal_cooler_data *)PDE_DATA(file_inode(file));
  1010. mutex_unlock(&MTM_COOLER_LOCK);
  1011. if (NULL == mcdata) {
  1012. THRML_ERROR_LOG("%s null data\n", __func__);
  1013. return -EINVAL;
  1014. }
  1015. /* WARNING: Modify here if MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS is changed to other than 3 */
  1016. #if (3 == MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS)
  1017. _mtkthermal_clear_cooler_conditions(mcdata);
  1018. if (2 <= sscanf(desc, "%s %d %s %d %s %d",
  1019. &mcdata->conditions[0][0], &mcdata->threshold[0],
  1020. &mcdata->conditions[1][0], &mcdata->threshold[1],
  1021. &mcdata->conditions[2][0], &mcdata->threshold[2])) {
  1022. int i = 0;
  1023. for (; i < MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS; i++) {
  1024. if (0 == strncmp(mcdata->conditions[i], "EXIT", 4)) {
  1025. mcdata->exit_threshold = mcdata->threshold[i];
  1026. } else if (0 == strncmp(mcdata->conditions[i], "CPU0", 4)) {
  1027. mcdata->condition_last_value[i] = &nCPU_loading_sum;
  1028. g_check_cpu_info_flag |= (1ULL << mcdata->id);
  1029. THRML_LOG("%s cpu flag: %016llx, id=%d\n", __func__,
  1030. g_check_cpu_info_flag, mcdata->id);
  1031. } else if (0 == strncmp(mcdata->conditions[i], "BATCC", 5)) {
  1032. mcdata->condition_last_value[i] = &nBattCurrentCnsmpt;
  1033. g_check_batt_info_flag |= (1ULL << mcdata->id);
  1034. THRML_LOG("%s batt flag: %016llx, id=%d\n", __func__,
  1035. g_check_batt_info_flag, mcdata->id);
  1036. } else if (0 == strncmp(mcdata->conditions[i], "WIFI", 4)) {
  1037. mcdata->condition_last_value[i] = &nWifi_throughput;
  1038. g_check_wifi_info_flag |= (1ULL << mcdata->id);
  1039. THRML_LOG("%s wifi flag: %016llx, id=%d\n", __func__,
  1040. g_check_wifi_info_flag, mcdata->id);
  1041. } else if (0 == strncmp(mcdata->conditions[i], "MOBILE", 5)) {
  1042. mcdata->condition_last_value[i] = &nMobile_throughput;
  1043. g_check_mobile_info_flag |= (1ULL << mcdata->id);
  1044. THRML_LOG("%s mobile flag: %016llx, id=%d\n", __func__,
  1045. g_check_mobile_info_flag, mcdata->id);
  1046. } else {
  1047. /* normal thermal zones */
  1048. mcdata->condition_last_value[i] = NULL;
  1049. }
  1050. THRML_LOG("%s %d: %s %d %p %d.\n", __func__,
  1051. i, &mcdata->conditions[i][0], mcdata->conditions[i][0],
  1052. mcdata->condition_last_value[i], mcdata->threshold[0]);
  1053. }
  1054. _mtm_decide_new_delay();
  1055. return count;
  1056. }
  1057. #else
  1058. #error "Change correspondent part when changing MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS!"
  1059. #endif
  1060. THRML_ERROR_LOG("%s bad arg\n", __func__);
  1061. return -EINVAL;
  1062. }
  1063. static int _mtkthermal_cooler_open(struct inode *inode, struct file *file)
  1064. {
  1065. return single_open(file, _mtkthermal_cooler_read, PDE_DATA(inode));
  1066. }
  1067. static const struct file_operations _mtkthermal_cooler_fops = {
  1068. .owner = THIS_MODULE,
  1069. .open = _mtkthermal_cooler_open,
  1070. .read = seq_read,
  1071. .llseek = seq_lseek,
  1072. .write = _mtkthermal_cooler_write,
  1073. .release = single_release,
  1074. };
  1075. static int _mtkthermal_tz_read(struct seq_file *m, void *v)
  1076. {
  1077. struct thermal_zone_device *tz = NULL;
  1078. if (NULL == m->private) {
  1079. THRML_ERROR_LOG("%s null data\n", __func__);
  1080. } else {
  1081. tz = (struct thermal_zone_device *)m->private;
  1082. /* TODO: consider the case that tz is unregistered... */
  1083. seq_printf(m, "%d\n", tz->temperature);
  1084. {
  1085. struct mtk_thermal_tz_data *tzdata = NULL;
  1086. int ma_len = 0;
  1087. int fake_temp = 0;
  1088. tzdata = tz->devdata;
  1089. if (!tzdata)
  1090. BUG();
  1091. #if (MAX_STEP_MA_LEN > 1)
  1092. mutex_lock(&tzdata->ma_lock);
  1093. ma_len = tzdata->ma_len;
  1094. fake_temp = tzdata->fake_temp;
  1095. seq_printf(m, "ma_len=%d\n", ma_len);
  1096. seq_printf(m, "%d ", tzdata->ma_lens[0]);
  1097. {
  1098. int i = 1;
  1099. for (; i < MAX_STEP_MA_LEN; i++)
  1100. seq_printf(m, "(%ld,%d) ", tzdata->msma_ht[i - 1],
  1101. tzdata->ma_lens[i]);
  1102. }
  1103. mutex_unlock(&tzdata->ma_lock);
  1104. seq_puts(m, "\n");
  1105. #else
  1106. mutex_lock(&tzdata->ma_lock);
  1107. ma_len = tzdata->ma_len;
  1108. fake_temp = tzdata->fake_temp;
  1109. mutex_unlock(&tzdata->ma_lock);
  1110. seq_printf(m, "ma_len=%d\n", ma_len);
  1111. #endif
  1112. if (-275000 < fake_temp) {
  1113. /* print Tfake only when fake_temp > -275000 */
  1114. seq_printf(m, "Tfake=%d\n", fake_temp);
  1115. }
  1116. }
  1117. }
  1118. return 0;
  1119. }
  1120. static ssize_t _mtkthermal_tz_write(struct file *file, const char __user *buffer, size_t count,
  1121. loff_t *data)
  1122. {
  1123. int len = 0;
  1124. char desc[128];
  1125. char trailing[128] = { 0 };
  1126. int check = 0;
  1127. struct thermal_zone_device *tz;
  1128. char arg_name[32] = { 0 };
  1129. int arg_val = 0;
  1130. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  1131. if (copy_from_user(desc, buffer, len))
  1132. return 0;
  1133. desc[len] = '\0';
  1134. tz = (struct thermal_zone_device *)PDE_DATA(file_inode(file));
  1135. if (NULL == tz) {
  1136. THRML_ERROR_LOG("%s null data\n", __func__);
  1137. return -EINVAL;
  1138. }
  1139. if (2 <= sscanf(desc, "%s %d %s", arg_name, &arg_val, trailing)) {
  1140. if ((0 == strncmp(arg_name, "ma_len", 6)) && (arg_val >= 1) && (arg_val <= 60)) {
  1141. struct mtk_thermal_tz_data *tzdata = NULL;
  1142. tzdata = tz->devdata;
  1143. if (!tzdata)
  1144. BUG();
  1145. THRML_ERROR_LOG("%s trailing=%s\n", __func__, trailing);
  1146. /**
  1147. * reset MA len and lock
  1148. */
  1149. #if (MAX_STEP_MA_LEN > 1)
  1150. mutex_lock(&tzdata->ma_lock);
  1151. tzdata->ma_len = arg_val;
  1152. tzdata->ma_counter = 0;
  1153. tzdata->curr_idx_ma_len = 0;
  1154. tzdata->ma_lens[0] = arg_val;
  1155. tzdata->msma_ht[0] = MSMA_MAX_HT;
  1156. THRML_ERROR_LOG("%s %s ma_len=%d.\n", __func__, tz->type, tzdata->ma_len);
  1157. #if (MAX_STEP_MA_LEN == 4)
  1158. /* reset */
  1159. tzdata->msma_ht[1] = tzdata->msma_ht[2] = tzdata->msma_ht[3] = MSMA_MAX_HT;
  1160. tzdata->ma_lens[1] = tzdata->ma_lens[2] = tzdata->ma_lens[3] = 1;
  1161. check = sscanf(trailing, "%ld,%d;%ld,%d;%ld,%d;", &tzdata->msma_ht[0], &tzdata->ma_lens[1],
  1162. &tzdata->msma_ht[1], &tzdata->ma_lens[2],
  1163. &tzdata->msma_ht[2], &tzdata->ma_lens[3]);
  1164. THRML_ERROR_LOG("%s %s (%ld, %d), (%ld, %d), (%ld, %d)\n", __func__,
  1165. tz->type, tzdata->msma_ht[0], tzdata->ma_lens[1],
  1166. tzdata->msma_ht[1], tzdata->ma_lens[2],
  1167. tzdata->msma_ht[2], tzdata->ma_lens[3]);
  1168. #else
  1169. #error
  1170. #endif
  1171. mutex_unlock(&tzdata->ma_lock);
  1172. #else
  1173. mutex_lock(&tzdata->ma_lock);
  1174. tzdata->ma_len = arg_val;
  1175. tzdata->ma_counter = 0;
  1176. mutex_unlock(&tzdata->ma_lock);
  1177. THRML_ERROR_LOG("%s %s ma_len=%d.\n", __func__, tz->type, tzdata->ma_len);
  1178. #endif
  1179. } else if ((0 == strncmp(arg_name, "Tfake", 5)) && (arg_val >= -275000)) {
  1180. /* only accept for [-275000, max positive value of int] */
  1181. struct mtk_thermal_tz_data *tzdata = NULL;
  1182. tzdata = tz->devdata;
  1183. if (!tzdata)
  1184. BUG();
  1185. mutex_lock(&tzdata->ma_lock);
  1186. tzdata->fake_temp = (long)arg_val;
  1187. mutex_unlock(&tzdata->ma_lock);
  1188. THRML_ERROR_LOG("%s %s Tfake=%ld.\n", __func__, tz->type, tzdata->fake_temp);
  1189. }
  1190. return count;
  1191. } else {
  1192. return -EINVAL;
  1193. }
  1194. }
  1195. static int _mtkthermal_tz_open(struct inode *inode, struct file *file)
  1196. {
  1197. return single_open(file, _mtkthermal_tz_read, PDE_DATA(inode));
  1198. }
  1199. static const struct file_operations _mtkthermal_tz_fops = {
  1200. .owner = THIS_MODULE,
  1201. .open = _mtkthermal_tz_open,
  1202. .read = seq_read,
  1203. .llseek = seq_lseek,
  1204. .write = _mtkthermal_tz_write,
  1205. .release = single_release,
  1206. };
  1207. #define MIN(_a_, _b_) ((_a_) < (_b_) ? (_a_) : (_b_))
  1208. /* No parameter check in this internal function */
  1209. static long _mtkthermal_update_and_get_sma(struct mtk_thermal_tz_data *tzdata, long latest_val)
  1210. {
  1211. long ret = 0;
  1212. if (NULL == tzdata) {
  1213. BUG();
  1214. return latest_val;
  1215. }
  1216. mutex_lock(&tzdata->ma_lock);
  1217. /* Use Tfake if set... */
  1218. latest_val = (-275000 < tzdata->fake_temp) ? tzdata->fake_temp : latest_val;
  1219. if (1 == tzdata->ma_len) {
  1220. ret = latest_val;
  1221. } else if (1 < tzdata->ma_len) {
  1222. int i = 0;
  1223. tzdata->ma[(tzdata->ma_counter) % (tzdata->ma_len)] = latest_val;
  1224. tzdata->ma_counter++;
  1225. for (i = 0; i < MIN(tzdata->ma_counter, tzdata->ma_len); i++)
  1226. ret += tzdata->ma[i];
  1227. ret = ret / ((long)MIN(tzdata->ma_counter, tzdata->ma_len));
  1228. }
  1229. #if (MAX_STEP_MA_LEN > 1)
  1230. /*
  1231. * 2. Move to correct region if ma_counter == 1
  1232. * a. For (i=0;SMA >= high_threshold[i];i++) ;
  1233. * b. if (curr_idx_sma_len != i) {ma_counter = 0; ma_len = sma_len[curr_idx_sma_len = i]; }
  1234. * 3. Check if need to change region if ma_counter > 1
  1235. * a. if SMA >= high_threshold[curr_idx_sma_len]
  1236. * { Move upward: ma_counter = 0; ma_len = sma_len[++curr_idx_sma_len]; }
  1237. * b. else if curr_idx_sma_len >0 && SMA < high_threshold[curr_idx_sma_len-1]
  1238. * { Move downward: ma_counter =0; ma_len = sma_len[--curr_idx_sma_len]; }
  1239. */
  1240. if (1 == tzdata->ma_counter) {
  1241. int i = 0;
  1242. for (; ret >= tzdata->msma_ht[i]; i++)
  1243. ;
  1244. if (tzdata->curr_idx_ma_len != i) {
  1245. tzdata->ma_counter = 0;
  1246. tzdata->ma_len = tzdata->ma_lens[tzdata->curr_idx_ma_len = i];
  1247. THRML_LOG("%s 2b ma_len: %d curr_idx_ma_len: %d\n", __func__,
  1248. tzdata->ma_len, tzdata->curr_idx_ma_len);
  1249. }
  1250. } else {
  1251. if (ret >= tzdata->msma_ht[tzdata->curr_idx_ma_len]) {
  1252. tzdata->ma_counter = 0;
  1253. tzdata->ma_len = tzdata->ma_lens[++(tzdata->curr_idx_ma_len)];
  1254. THRML_LOG("%s 3a ma_len: %d curr_idx_ma_len: %d\n", __func__,
  1255. tzdata->ma_len, tzdata->curr_idx_ma_len);
  1256. } else if (tzdata->curr_idx_ma_len > 0
  1257. && ret < tzdata->msma_ht[tzdata->curr_idx_ma_len - 1]) {
  1258. tzdata->ma_counter = 0;
  1259. tzdata->ma_len = tzdata->ma_lens[--(tzdata->curr_idx_ma_len)];
  1260. THRML_LOG("%s 3b ma_len: %d curr_idx_ma_len: %d\n", __func__,
  1261. tzdata->ma_len, tzdata->curr_idx_ma_len);
  1262. }
  1263. }
  1264. #endif
  1265. mutex_unlock(&tzdata->ma_lock);
  1266. return ret;
  1267. }
  1268. /**
  1269. * 0: means please do not show thermal limit in "Show CPU Usage" panel.
  1270. * 1: means show thermal limit and CPU temp only
  1271. * 2: means show all all tz temp besides thermal limit and CPU temp
  1272. */
  1273. static unsigned int g_thermal_indicator_mode;
  1274. /**
  1275. * delay in milliseconds.
  1276. */
  1277. static unsigned int g_thermal_indicator_delay;
  1278. /* Read */
  1279. static int _mtkthermal_indicator_read(struct seq_file *m, void *v)
  1280. {
  1281. seq_printf(m, "%d\n%d\n", g_thermal_indicator_mode, g_thermal_indicator_delay);
  1282. return 0;
  1283. }
  1284. /* Write */
  1285. static ssize_t _mtkthermal_indicator_write(struct file *file, const char __user *buffer,
  1286. size_t count, loff_t *data)
  1287. {
  1288. int len = 0, thermal_indicator_mode = 0, thermal_indicator_delay = 0;
  1289. char desc[32];
  1290. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  1291. if (copy_from_user(desc, buffer, len))
  1292. return 0;
  1293. desc[len] = '\0';
  1294. if (sscanf(desc, "%d %d", &thermal_indicator_mode, &thermal_indicator_delay) == 2) {
  1295. if ((thermal_indicator_mode >= 0) && (thermal_indicator_mode <= 3))
  1296. g_thermal_indicator_mode = thermal_indicator_mode;
  1297. g_thermal_indicator_delay = thermal_indicator_delay;
  1298. return count;
  1299. } else {
  1300. return 0;
  1301. }
  1302. }
  1303. static int _mtkthermal_indicator_open(struct inode *inode, struct file *file)
  1304. {
  1305. return single_open(file, _mtkthermal_indicator_read, NULL);
  1306. }
  1307. static const struct file_operations _mtkthermal_indicator_fops = {
  1308. .owner = THIS_MODULE,
  1309. .open = _mtkthermal_indicator_open,
  1310. .read = seq_read,
  1311. .llseek = seq_lseek,
  1312. .write = _mtkthermal_indicator_write,
  1313. .release = single_release,
  1314. };
  1315. /* Read */
  1316. static int _mtm_scen_call_read(struct seq_file *m, void *v)
  1317. {
  1318. seq_printf(m, "%d\n", g_mtm_phone_call_ongoing);
  1319. return 0;
  1320. }
  1321. /* Write */
  1322. static ssize_t _mtm_scen_call_write(struct file *file, const char __user *buffer, size_t count,
  1323. loff_t *data)
  1324. {
  1325. int len = 0, mtm_phone_call_ongoing = 0;
  1326. char desc[32];
  1327. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  1328. if (copy_from_user(desc, buffer, len))
  1329. return 0;
  1330. desc[len] = '\0';
  1331. if (kstrtoint(desc, 10, &mtm_phone_call_ongoing) == 0) {
  1332. if ((mtm_phone_call_ongoing == 0) || (mtm_phone_call_ongoing == 1)) {
  1333. g_mtm_phone_call_ongoing = mtm_phone_call_ongoing;
  1334. if (1 == mtm_phone_call_ongoing)
  1335. mtk_thermal_set_user_scenarios(MTK_THERMAL_SCEN_CALL);
  1336. else if (0 == mtm_phone_call_ongoing)
  1337. mtk_thermal_clear_user_scenarios(MTK_THERMAL_SCEN_CALL);
  1338. }
  1339. return count;
  1340. }
  1341. return 0;
  1342. }
  1343. static int _mtm_scen_call_open(struct inode *inode, struct file *file)
  1344. {
  1345. return single_open(file, _mtm_scen_call_read, NULL);
  1346. }
  1347. static const struct file_operations _mtm_scen_call_fops = {
  1348. .owner = THIS_MODULE,
  1349. .open = _mtm_scen_call_open,
  1350. .read = seq_read,
  1351. .llseek = seq_lseek,
  1352. .write = _mtm_scen_call_write,
  1353. .release = single_release,
  1354. };
  1355. /* Init */
  1356. static int __init mtkthermal_init(void)
  1357. {
  1358. int err = 0;
  1359. struct proc_dir_entry *entry;
  1360. struct proc_dir_entry *dir_entry = mtk_thermal_get_proc_drv_therm_dir_entry();
  1361. THRML_LOG("%s\n", __func__);
  1362. entry = proc_create("mtm_monitor", S_IRUGO | S_IWUSR | S_IWGRP, dir_entry, &mtkthermal_fops);
  1363. if (!entry)
  1364. THRML_ERROR_LOG("%s Can not create mtm_monitor\n", __func__);
  1365. else
  1366. proc_set_user(entry, uid, gid);
  1367. entry = proc_create("mtm_indicator", S_IRUGO | S_IWUSR, dir_entry, &_mtkthermal_indicator_fops);
  1368. if (!entry)
  1369. THRML_ERROR_LOG("%s Can not create mtm_indicator\n", __func__);
  1370. entry = proc_create("mtm_scen_call", S_IRUGO | S_IWUSR | S_IWGRP, dir_entry, &_mtm_scen_call_fops);
  1371. if (!entry)
  1372. THRML_ERROR_LOG("%s Can not create mtm_scen_call\n", __func__);
  1373. else
  1374. proc_set_user(entry, uid, gid);
  1375. /* create /proc/cooler folder */
  1376. /* WARNING! This is not gauranteed to be invoked before mtk_ts_cpu's functions... */
  1377. proc_cooler_dir_entry =
  1378. (NULL == proc_cooler_dir_entry) ? proc_mkdir("mtkcooler", NULL) : proc_cooler_dir_entry;
  1379. if (NULL == proc_cooler_dir_entry)
  1380. THRML_ERROR_LOG("%s mkdir /proc/mtkcooler failed\n", __func__);
  1381. /* create /proc/tz folder */
  1382. /* WARNING! This is not gauranteed to be invoked before mtk_ts_cpu's functions... */
  1383. proc_tz_dir_entry =
  1384. (NULL == proc_tz_dir_entry) ? proc_mkdir("mtktz", NULL) : proc_tz_dir_entry;
  1385. if (NULL == proc_tz_dir_entry)
  1386. THRML_ERROR_LOG("%s mkdir /proc/mtktz failed\n", __func__);
  1387. #if defined(CONFIG_MTK_THERMAL_TIME_BASE_PROTECTION)
  1388. wake_lock_init(&mtm_wake_lock, WAKE_LOCK_SUSPEND, "alarm");
  1389. #endif
  1390. #ifdef CONFIG_MTK_THERMAL_EXT_CONTROL
  1391. g_controlState = MTK_THERMAL_CONTROL_STATE_POLLING;
  1392. memset(&mtk_thermal_ext_tz_values, 0,
  1393. sizeof(struct mtk_thermal_ext_tz_data) * MTK_THERMAL_EXT_SENSOR_COUNT);
  1394. entry =
  1395. proc_create("mtm_extctrl", S_IRUGO | S_IWUSR, dir_entry, &mtk_thermal_ext_proc_fops);
  1396. if (!entry)
  1397. THRML_ERROR_LOG("%s Can not create mtm_extctrl\n", __func__);
  1398. /* Register AP side IPI handler */
  1399. THRML_LOG("%s Register AP side IPI handler\n", __func__);
  1400. md32_ipi_registration(IPI_THERMAL, mtk_thermal_ext_ipi_msg_handler, "Thermal");
  1401. md32_register_notify(&mtk_thermal_ext_nb);
  1402. #endif
  1403. INIT_DELAYED_WORK(&_mtm_sysinfo_poll_queue, _mtm_update_sysinfo);
  1404. _mtm_update_sysinfo(NULL);
  1405. return err;
  1406. }
  1407. /* Exit */
  1408. static void __exit mtkthermal_exit(void)
  1409. {
  1410. THRML_LOG("%s\n", __func__);
  1411. #if defined(CONFIG_MTK_THERMAL_TIME_BASE_PROTECTION)
  1412. wake_lock_destroy(&mtm_wake_lock);
  1413. #endif
  1414. #ifdef CONFIG_MTK_THERMAL_EXT_CONTROL
  1415. mutex_destroy(&mtk_thermal_ext_control_lock);
  1416. #endif
  1417. }
  1418. #if defined(CONFIG_MTK_THERMAL_TIME_BASE_PROTECTION)
  1419. static int __init mtkthermal_late_init(void)
  1420. {
  1421. THRML_LOG("%s\n", __func__);
  1422. return platform_driver_register(&mtk_thermal_monitor_driver);
  1423. }
  1424. #endif
  1425. /* ************************************ */
  1426. /* thermal_zone_device_ops Wrapper */
  1427. /* ************************************ */
  1428. /*
  1429. * .bind wrapper: bind the thermal zone device with a thermal cooling device.
  1430. */
  1431. static int mtk_thermal_wrapper_bind(struct thermal_zone_device *thermal, struct thermal_cooling_device *cdev)
  1432. {
  1433. int ret = 0;
  1434. struct thermal_zone_device_ops *ops;
  1435. /* WARNING! bind will invoke mtk_thermal_zone_bind_cooling_device_wrapper(),
  1436. so don't rollback cooler's devdata in this bind... */
  1437. #if MTK_THERMAL_MONITOR_CONDITIONAL_COOLING
  1438. {
  1439. int i = 0;
  1440. struct mtk_thermal_cooler_data *cldata = NULL;
  1441. mutex_lock(&MTM_COOLER_LOCK);
  1442. cldata = cdev->devdata;
  1443. mutex_unlock(&MTM_COOLER_LOCK);
  1444. for (; i < MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS; i++) {
  1445. if ((0x0 != cldata->conditions[i][0]) &&
  1446. (NULL == cldata->condition_last_value[i])) {
  1447. if (0 == strncmp(cldata->conditions[i], thermal->type, 20)) {
  1448. cldata->condition_last_value[i] = &(thermal->temperature);
  1449. THRML_LOG
  1450. ("[.bind]condition+ tz: %s cdev: %s condition: %s\n",
  1451. thermal->type, cdev->type, cldata->conditions[i]);
  1452. }
  1453. }
  1454. }
  1455. }
  1456. #endif
  1457. /* Bind Relationship to StoreLogger */
  1458. THRML_LOG("[.bind]+ tz: %s cdev: %s tz_data:%p cl_data:%p\n", thermal->type, cdev->type,
  1459. thermal->devdata, cdev->devdata);
  1460. ops = getClientZoneOps(thermal);
  1461. if (!ops) {
  1462. THRML_ERROR_LOG("[.bind]E tz: %s unregistered.\n", thermal->type);
  1463. return 1;
  1464. }
  1465. if (ops->bind)
  1466. ops->bind(thermal, cdev);
  1467. /* Bind Relationship to StoreLogger */
  1468. THRML_LOG("[.bind]- tz: %s cdev: %s tz_data:%p cl_data:%p\n", thermal->type, cdev->type,
  1469. thermal->devdata, cdev->devdata);
  1470. /* Log in mtk_thermal_zone_bind_cooling_device_wrapper() */
  1471. /* THRML_STORAGE_LOG(THRML_LOGGER_MSG_BIND, bind, thermal->type, cdev->type); */
  1472. return ret;
  1473. }
  1474. /*
  1475. *.unbind wrapper: unbind the thermal zone device with a thermal cooling device.
  1476. */
  1477. static int mtk_thermal_wrapper_unbind
  1478. (struct thermal_zone_device *thermal, struct thermal_cooling_device *cdev) {
  1479. int ret = 0;
  1480. struct thermal_zone_device_ops *ops;
  1481. #if MTK_THERMAL_MONITOR_CONDITIONAL_COOLING
  1482. {
  1483. int i = 0;
  1484. struct mtk_thermal_cooler_data *cldata = NULL;
  1485. mutex_lock(&MTM_COOLER_LOCK);
  1486. cldata = cdev->devdata;
  1487. mutex_unlock(&MTM_COOLER_LOCK);
  1488. /* Clear cldata->tz */
  1489. if (thermal == cldata->tz) {
  1490. /* clear the state of cooler bounded first... */
  1491. if (cdev->ops)
  1492. cdev->ops->set_cur_state(cdev, 0);
  1493. cldata->tz = NULL;
  1494. cldata->trip = 0;
  1495. }
  1496. for (; i < MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS; i++) {
  1497. if ((NULL != cldata->condition_last_value[i]) &&
  1498. (&(thermal->temperature) == cldata->condition_last_value[i])) {
  1499. cldata->condition_last_value[i] = NULL;
  1500. THRML_LOG("[.unbind]condition- tz: %s cdev: %s condition: %s\n",
  1501. thermal->type, cdev->type, cldata->conditions[i]);
  1502. }
  1503. }
  1504. }
  1505. #endif
  1506. THRML_LOG("[.unbind]+ tz: %s cdev: %s\n", thermal->type, cdev->type);
  1507. ops = getClientZoneOps(thermal);
  1508. if (!ops) {
  1509. THRML_ERROR_LOG("[.unbind]E tz: %s unregistered.\n", thermal->type);
  1510. return 1;
  1511. }
  1512. if (ops->unbind)
  1513. ret = ops->unbind(thermal, cdev);
  1514. THRML_LOG("[.unbind]- tz: %s cdev: %s\n", thermal->type, cdev->type);
  1515. return ret;
  1516. }
  1517. /*
  1518. * .get_temp wrapper: get the current temperature of the thermal zone.
  1519. */
  1520. static int mtk_thermal_wrapper_get_temp
  1521. (struct thermal_zone_device *thermal, unsigned long *temperature) {
  1522. int ret = 0;
  1523. struct thermal_zone_device_ops *ops;
  1524. int nTemperature;
  1525. unsigned long raw_temp = 0;
  1526. #if MTK_THERMAL_MONITOR_MEASURE_GET_TEMP_OVERHEAD
  1527. long int t = _get_current_time_us();
  1528. long int dur = 0;
  1529. #endif
  1530. ops = getClientZoneOps(thermal);
  1531. if (!ops) {
  1532. THRML_ERROR_LOG("[.get_temp] tz: %s unregistered.\n", thermal->type);
  1533. return 1;
  1534. }
  1535. #ifndef CONFIG_MTK_THERMAL_EXT_CONTROL
  1536. if (ops->get_temp)
  1537. ret = ops->get_temp(thermal, &raw_temp);
  1538. #else
  1539. if (mtk_thermal_ext_get_temp(thermal, &raw_temp) < 0) {
  1540. if (ops->get_temp)
  1541. ret = ops->get_temp(thermal, &raw_temp);
  1542. }
  1543. #endif
  1544. nTemperature = (int)raw_temp; /* /< Long cast to INT. */
  1545. #if defined(CONFIG_MTK_THERMAL_TIME_BASE_PROTECTION)
  1546. /* if batt temp raw data < 60C, release wake lock */
  1547. if ((tz_last_values[MTK_THERMAL_SENSOR_BATTERY] != NULL) && /* batt TZ is registered */
  1548. (&(thermal->temperature) == tz_last_values[MTK_THERMAL_SENSOR_BATTERY])) { /* get batt temp this time */
  1549. if (wake_lock_active(&mtm_wake_lock)) {
  1550. nTemperature = mtk_thermal_force_get_batt_temp() * 1000;
  1551. raw_temp = nTemperature;
  1552. THRML_ERROR_LOG("[.get_temp] tz: %s wake_lock_active() batt temp=%d\n",
  1553. thermal->type, nTemperature);
  1554. }
  1555. if (nTemperature < 59000 && wake_lock_active(&mtm_wake_lock)) {
  1556. /* unlock when only batt temp below 60C */
  1557. THRML_ERROR_LOG("[.get_temp] tz: %s wake_unlock()\n", thermal->type);
  1558. wake_unlock(&mtm_wake_lock);
  1559. }
  1560. last_batt_raw_temp = nTemperature;
  1561. }
  1562. #endif
  1563. if (0 == ret) {
  1564. *temperature = _mtkthermal_update_and_get_sma(thermal->devdata, raw_temp);
  1565. /* No strong type cast... */
  1566. } else {
  1567. THRML_ERROR_LOG("[.get_temp] tz: %s invalid temp\n", thermal->type);
  1568. *temperature = nTemperature;
  1569. }
  1570. /* Monitor Temperature to StoreLogger */
  1571. THRML_STORAGE_LOG(THRML_LOGGER_MSG_ZONE_TEMP, get_temp, thermal->type, (int)*temperature);
  1572. THRML_LOG("[.get_temp] tz: %s raw: %d sma: %ld\n", thermal->type, nTemperature,
  1573. (long)*temperature);
  1574. #if MTK_THERMAL_MONITOR_MEASURE_GET_TEMP_OVERHEAD
  1575. dur = _get_current_time_us() - t;
  1576. if (dur > 10000) /* over 10msec, log it */
  1577. THRML_ERROR_LOG("[.get_temp] tz: %s dur: %ld\n", thermal->type, dur);
  1578. #endif
  1579. return ret;
  1580. }
  1581. /*
  1582. * .get_mode wrapper: get the current mode (user/kernel) of the thermal zone.
  1583. * - "kernel" means thermal management is done in kernel.
  1584. * - "user" will prevent kernel thermal driver actions upon trip points
  1585. */
  1586. static int mtk_thermal_wrapper_get_mode
  1587. (struct thermal_zone_device *thermal, enum thermal_device_mode *mode) {
  1588. int ret = 0;
  1589. struct thermal_zone_device_ops *ops;
  1590. THRML_LOG("[.get_mode] tz: %s mode: %d\n", thermal->type, *mode);
  1591. ops = getClientZoneOps(thermal);
  1592. if (!ops) {
  1593. THRML_ERROR_LOG("[.get_mode] tz: %s unregistered.\n", thermal->type);
  1594. return 1;
  1595. }
  1596. if (ops->get_mode)
  1597. ret = ops->get_mode(thermal, mode);
  1598. return ret;
  1599. }
  1600. /*
  1601. * .set_mode wrapper: set the mode (user/kernel) of the thermal zone.
  1602. */
  1603. static int mtk_thermal_wrapper_set_mode
  1604. (struct thermal_zone_device *thermal, enum thermal_device_mode mode) {
  1605. int ret = 0;
  1606. struct thermal_zone_device_ops *ops;
  1607. THRML_LOG("[.set_mode] tz: %s mode: %d\n", thermal->type, mode);
  1608. ops = getClientZoneOps(thermal);
  1609. if (!ops) {
  1610. THRML_ERROR_LOG("[.set_mode] tz: %s unregistered.\n", thermal->type);
  1611. return 1;
  1612. }
  1613. if (ops->set_mode)
  1614. ret = ops->set_mode(thermal, mode);
  1615. return ret;
  1616. }
  1617. /*
  1618. * .get_trip_type wrapper: get the type of certain trip point.
  1619. */
  1620. static int mtk_thermal_wrapper_get_trip_type
  1621. (struct thermal_zone_device *thermal, int trip, enum thermal_trip_type *type) {
  1622. int ret = 0;
  1623. struct thermal_zone_device_ops *ops;
  1624. ops = getClientZoneOps(thermal);
  1625. if (!ops) {
  1626. THRML_ERROR_LOG("[.get_trip_type] tz: %s unregistered.\n", thermal->type);
  1627. return 1;
  1628. }
  1629. if (ops->get_trip_type)
  1630. ret = ops->get_trip_type(thermal, trip, type);
  1631. THRML_LOG("[.get_trip_type] tz: %s trip: %d type: %d\n", thermal->type, trip, *type);
  1632. return ret;
  1633. }
  1634. /*
  1635. * .get_trip_temp wrapper: get the temperature above which the certain trip point
  1636. * will be fired.
  1637. */
  1638. static int mtk_thermal_wrapper_get_trip_temp
  1639. (struct thermal_zone_device *thermal, int trip, unsigned long *temperature) {
  1640. int ret = 0;
  1641. struct thermal_zone_device_ops *ops;
  1642. ops = getClientZoneOps(thermal);
  1643. if (!ops) {
  1644. THRML_ERROR_LOG("[.get_trip_temp] tz: %s unregistered.\n", thermal->type);
  1645. return 1;
  1646. }
  1647. if (ops->get_trip_temp)
  1648. ret = ops->get_trip_temp(thermal, trip, temperature);
  1649. THRML_LOG("[.get_trip_temp] tz: %s trip: %d temp: %ld\n", thermal->type, trip,
  1650. (long)*temperature);
  1651. THRML_STORAGE_LOG(THRML_LOGGER_MSG_TRIP_POINT, get_trip_temp, thermal->type, trip,
  1652. *temperature);
  1653. return ret;
  1654. }
  1655. /*
  1656. * .get_crit_temp wrapper:
  1657. */
  1658. static int mtk_thermal_wrapper_get_crit_temp
  1659. (struct thermal_zone_device *thermal, unsigned long *temperature) {
  1660. int ret = 0;
  1661. struct thermal_zone_device_ops *ops;
  1662. ops = getClientZoneOps(thermal);
  1663. if (!ops) {
  1664. THRML_ERROR_LOG("[.get_crit_temp] tz: %s unregistered.\n", thermal->type);
  1665. return 1;
  1666. }
  1667. if (ops->get_crit_temp)
  1668. ret = ops->get_crit_temp(thermal, temperature);
  1669. THRML_LOG("[.get_crit_temp] tz: %s temp: %ld\n", thermal->type, (long)*temperature);
  1670. return ret;
  1671. }
  1672. static int mtk_thermal_wrapper_notify
  1673. (struct thermal_zone_device *thermal, int trip, enum thermal_trip_type type) {
  1674. int ret = 0;
  1675. struct thermal_zone_device_ops *ops;
  1676. ops = getClientZoneOps(thermal);
  1677. if (!ops) {
  1678. THRML_ERROR_LOG("[.notify] tz: %s unregistered.\n", thermal->type);
  1679. return 1;
  1680. }
  1681. if (ops->notify)
  1682. ret = ops->notify(thermal, trip, type);
  1683. return ret;
  1684. }
  1685. /* *************************************** */
  1686. /* MTK thermal zone register/unregister */
  1687. /* *************************************** */
  1688. /* Wrapper callback OPS */
  1689. static struct thermal_zone_device_ops mtk_thermal_wrapper_dev_ops = {
  1690. .bind = mtk_thermal_wrapper_bind,
  1691. .unbind = mtk_thermal_wrapper_unbind,
  1692. .get_temp = mtk_thermal_wrapper_get_temp,
  1693. .get_mode = mtk_thermal_wrapper_get_mode,
  1694. .set_mode = mtk_thermal_wrapper_set_mode,
  1695. .get_trip_type = mtk_thermal_wrapper_get_trip_type,
  1696. .get_trip_temp = mtk_thermal_wrapper_get_trip_temp,
  1697. .get_crit_temp = mtk_thermal_wrapper_get_crit_temp,
  1698. .notify = mtk_thermal_wrapper_notify,
  1699. };
  1700. /*mtk thermal zone register function */
  1701. struct thermal_zone_device *mtk_thermal_zone_device_register_wrapper
  1702. (char *type, int trips, void *devdata, const struct thermal_zone_device_ops *ops,
  1703. int tc1, int tc2, int passive_delay, int polling_delay) {
  1704. struct thermal_zone_device *tz = NULL;
  1705. struct mtk_thermal_tz_data *tzdata = NULL;
  1706. int tzidx;
  1707. THRML_LOG("%s tz: %s trips: %d passive_delay: %d polling_delay: %d\n", __func__, type,
  1708. trips, passive_delay, polling_delay);
  1709. if (strcmp(SYSINFO_ATTACH_DEV_NAME, type) == 0)
  1710. g_SysinfoAttachOps = (struct thermal_zone_device_ops *)ops;
  1711. tzdata = kzalloc(sizeof(struct mtk_thermal_tz_data), GFP_KERNEL);
  1712. if (!tzdata) {
  1713. THRML_ERROR_LOG("%s tzdata kzalloc fail.\n", __func__);
  1714. return ERR_PTR(-ENOMEM);
  1715. }
  1716. mutex_init(&tzdata->ma_lock);
  1717. mutex_lock(&tzdata->ma_lock);
  1718. tzdata->ops = (struct thermal_zone_device_ops *)ops;
  1719. tzdata->ma_len = 1;
  1720. tzdata->ma_counter = 0;
  1721. tzdata->fake_temp = -275000; /* init to -275000 */
  1722. #if (MAX_STEP_MA_LEN > 1)
  1723. tzdata->curr_idx_ma_len = 0;
  1724. tzdata->ma_lens[0] = 1;
  1725. tzdata->msma_ht[0] = MSMA_MAX_HT;
  1726. #endif
  1727. mb();
  1728. mutex_unlock(&tzdata->ma_lock);
  1729. tz = thermal_zone_device_register(type, trips, /* /< total number of trip points */
  1730. 0, /* /< mask */
  1731. /* (void*)ops, ///< invoker's ops pass to devdata */
  1732. (void *)tzdata, &mtk_thermal_wrapper_dev_ops, /* /< use wrapper ops. */
  1733. NULL, /* /< tzp */
  1734. passive_delay, polling_delay);
  1735. tzidx = mtk_thermal_get_tz_idx(type);
  1736. /* registered the last_temperature to local arra */
  1737. mutex_lock(&MTM_GET_TEMP_LOCK);
  1738. {
  1739. if (tzidx >= 0 && tzidx < MTK_THERMAL_SENSOR_COUNT)
  1740. tz_last_values[tzidx] = &(tz->temperature);
  1741. }
  1742. mutex_unlock(&MTM_GET_TEMP_LOCK);
  1743. #ifdef CONFIG_MTK_THERMAL_EXT_CONTROL
  1744. tzidx = mtk_thermal_ext_get_tz_idx(type);
  1745. if (tzidx >= 0 && tzidx < MTK_THERMAL_EXT_SENSOR_COUNT) {
  1746. mutex_lock(&mtk_thermal_ext_control_lock);
  1747. mtk_thermal_ext_tz_values[tzidx].tz = tz;
  1748. mtk_thermal_ext_tz_values[tzidx].tzdata = tzdata;
  1749. mtk_thermal_ext_tz_values[tzidx].trips = trips;
  1750. mtk_thermal_ext_tz_values[tzidx].last_temperature = *tz_last_values[tzidx];
  1751. mtk_thermal_ext_tz_values[tzidx].polling_delay = polling_delay;
  1752. if (mtk_thermal_ext_get_threshold(&mtk_thermal_ext_tz_values[tzidx], tz, ops, trips)
  1753. < 0) {
  1754. mtk_thermal_ext_tz_values[tzidx].high_trip_point =
  1755. MTK_THERMAL_DEFAULT_MAX_TEMPERATURE;
  1756. mtk_thermal_ext_tz_values[tzidx].low_trip_point =
  1757. MTK_THERMAL_DEFAULT_MAX_TEMPERATURE;
  1758. }
  1759. mtk_thermal_ext_tz_values[tzidx].set = true;
  1760. THRML_LOG
  1761. ("%s %s, id: %d, temp: %d, polling delay: %d, low trip: %d, high trip: %d\n",
  1762. __func__, type, tzidx, mtk_thermal_ext_tz_values[tzidx].last_temperature,
  1763. mtk_thermal_ext_tz_values[tzidx].polling_delay,
  1764. mtk_thermal_ext_tz_values[tzidx].low_trip_point,
  1765. mtk_thermal_ext_tz_values[tzidx].high_trip_point);
  1766. if (g_controlState == MTK_THERMAL_CONTROL_STATE_INTERRUPT ||
  1767. g_controlState == MTK_THERMAL_CONTROL_STATE_SWITCHING) {
  1768. /* Set TZ high/low threshold to MD32 */
  1769. mtk_thermal_ext_set_tz_threshold(&mtk_thermal_ext_tz_values[tzidx], tzidx);
  1770. /* [Warning] Can not use cancel_delayed_work_sync() here
  1771. because it will cause kernel warning (LockProve Warning) */
  1772. if (g_controlState == MTK_THERMAL_CONTROL_STATE_INTERRUPT &&
  1773. cancel_delayed_work(&(tz->poll_queue)) == 0) {
  1774. THRML_ERROR_LOG("%s cancel tz %d work, work is running\n", __func__,
  1775. type);
  1776. }
  1777. }
  1778. mutex_unlock(&mtk_thermal_ext_control_lock);
  1779. }
  1780. #endif /* CONFIG_MTK_THERMAL_EXT_CONTROL */
  1781. /* create a proc for this tz... */
  1782. if (NULL != _get_proc_tz_dir_entry()) {
  1783. struct proc_dir_entry *entry;
  1784. entry =
  1785. proc_create_data((const char *)type, S_IRUGO | S_IWUSR | S_IWGRP,
  1786. proc_tz_dir_entry, &_mtkthermal_tz_fops, tz);
  1787. if (!entry) {
  1788. THRML_ERROR_LOG("%s proc file not created: %p\n", __func__, tz);
  1789. } else {
  1790. proc_set_user(entry, uid, gid);
  1791. THRML_LOG("%s proc file created: %p\n", __func__, tz);
  1792. }
  1793. }
  1794. /* This interface function adds a new thermal zone device */
  1795. return tz;
  1796. }
  1797. EXPORT_SYMBOL(mtk_thermal_zone_device_register_wrapper);
  1798. /*mtk thermal zone unregister function */
  1799. void mtk_thermal_zone_device_unregister_wrapper(struct thermal_zone_device *tz)
  1800. {
  1801. char type[32] = { 0 };
  1802. struct mtk_thermal_tz_data *tzdata = NULL;
  1803. int tzidx;
  1804. strncpy(type, tz->type, 20);
  1805. tzdata = (struct mtk_thermal_tz_data *)tz->devdata;
  1806. /* delete the proc file entry from proc */
  1807. if (NULL != proc_tz_dir_entry)
  1808. remove_proc_entry((const char *)type, proc_tz_dir_entry);
  1809. #ifdef CONFIG_MTK_THERMAL_EXT_CONTROL
  1810. tzidx = mtk_thermal_ext_get_tz_idx(tz->type);
  1811. mutex_lock(&mtk_thermal_ext_control_lock);
  1812. if (tzidx >= 0 && tzidx < MTK_THERMAL_EXT_SENSOR_COUNT) {
  1813. bool set = mtk_thermal_ext_tz_values[tzidx].set;
  1814. /* Unset MD32 TZ high/low threshold and polling delay */
  1815. memset(&mtk_thermal_ext_tz_values[tzidx], 0,
  1816. sizeof(struct mtk_thermal_ext_tz_data));
  1817. if (set)
  1818. mtk_thermal_ext_set_tz_threshold(&mtk_thermal_ext_tz_values[tzidx], tzidx);
  1819. }
  1820. mutex_unlock(&mtk_thermal_ext_control_lock);
  1821. #endif
  1822. tzidx = mtk_thermal_get_tz_idx(tz->type);
  1823. /* unregistered the last_temperature from local array */
  1824. mutex_lock(&MTM_GET_TEMP_LOCK);
  1825. {
  1826. if (tzidx >= 0 && tzidx < MTK_THERMAL_SENSOR_COUNT)
  1827. tz_last_values[tzidx] = NULL;
  1828. }
  1829. mutex_unlock(&MTM_GET_TEMP_LOCK);
  1830. THRML_LOG("%s+ tz : %s\n", __func__, type);
  1831. thermal_zone_device_unregister(tz);
  1832. THRML_LOG("%s- tz: %s\n", __func__, type);
  1833. /* free memory */
  1834. if (NULL != tzdata) {
  1835. mutex_lock(&tzdata->ma_lock);
  1836. tzdata->ops = NULL;
  1837. mutex_unlock(&tzdata->ma_lock);
  1838. mutex_destroy(&tzdata->ma_lock);
  1839. kfree(tzdata);
  1840. }
  1841. }
  1842. EXPORT_SYMBOL(mtk_thermal_zone_device_unregister_wrapper);
  1843. int mtk_thermal_zone_bind_cooling_device_wrapper(struct thermal_zone_device *thermal,
  1844. int trip, struct thermal_cooling_device *cdev) {
  1845. struct mtk_thermal_cooler_data *mcdata;
  1846. int ret = 0;
  1847. THRML_LOG("%s thermal_type:%s trip:%d cdev_type:%s ret:%d\n", __func__,
  1848. thermal->type, trip, cdev->type, ret);
  1849. ret =
  1850. thermal_zone_bind_cooling_device(thermal, trip, cdev, THERMAL_NO_LIMIT,
  1851. THERMAL_NO_LIMIT);
  1852. if (ret) {
  1853. THRML_ERROR_LOG("thermal_zone_bind_cooling_device Fail. Code(%d)\n", ret);
  1854. } else {
  1855. /* TODO: think of a way don't do this here...Or cannot rollback devdata in bind ops... */
  1856. /* Init mtk Cooler Data */
  1857. mcdata = cdev->devdata;
  1858. mcdata->trip = trip;
  1859. mcdata->tz = thermal;
  1860. }
  1861. THRML_LOG("%s thermal_type:%s trip:%d cdev_type:%s ret:%d\n", __func__,
  1862. thermal->type, trip, cdev->type, ret);
  1863. THRML_STORAGE_LOG(THRML_LOGGER_MSG_BIND, bind, thermal->type, trip, cdev->type);
  1864. return ret;
  1865. }
  1866. EXPORT_SYMBOL(mtk_thermal_zone_bind_cooling_device_wrapper);
  1867. /* ********************************************* */
  1868. /* MTK cooling dev register/unregister */
  1869. /* ********************************************* */
  1870. /* .get_max_state */
  1871. static int mtk_cooling_wrapper_get_max_state
  1872. (struct thermal_cooling_device *cdev, unsigned long *state) {
  1873. int ret = 0;
  1874. struct thermal_cooling_device_ops *ops;
  1875. struct mtk_thermal_cooler_data *mcdata;
  1876. mutex_lock(&MTM_COOLER_LOCK);
  1877. /* Recovery client's devdata */
  1878. ops = recoveryClientCooler(cdev, &mcdata);
  1879. if (ops->get_max_state)
  1880. ret = ops->get_max_state(cdev, state);
  1881. THRML_LOG("[.get_max_state] cdev_type:%s state:%lu\n", cdev->type, *state);
  1882. cdev->devdata = mcdata;
  1883. mutex_unlock(&MTM_COOLER_LOCK);
  1884. return ret;
  1885. }
  1886. /* .get_cur_state */
  1887. static int mtk_cooling_wrapper_get_cur_state
  1888. (struct thermal_cooling_device *cdev, unsigned long *state) {
  1889. int ret = 0;
  1890. struct thermal_cooling_device_ops *ops;
  1891. struct mtk_thermal_cooler_data *mcdata;
  1892. mutex_lock(&MTM_COOLER_LOCK);
  1893. /* Recovery client's devdata */
  1894. ops = recoveryClientCooler(cdev, &mcdata);
  1895. if (ops->get_cur_state)
  1896. ret = ops->get_cur_state(cdev, state);
  1897. THRML_LOG("[.get_cur_state] cdev_type:%s state:%lu\n", cdev->type, *state);
  1898. /* reset devdata to mcdata */
  1899. cdev->devdata = mcdata;
  1900. mutex_unlock(&MTM_COOLER_LOCK);
  1901. return ret;
  1902. }
  1903. /* set_cur_state */
  1904. static int mtk_cooling_wrapper_set_cur_state
  1905. (struct thermal_cooling_device *cdev, unsigned long state) {
  1906. struct thermal_cooling_device_ops *ops;
  1907. struct mtk_thermal_cooler_data *mcdata;
  1908. int ret = 0;
  1909. unsigned long cur_state = 0;
  1910. mutex_lock(&MTM_COOLER_LOCK);
  1911. /* Recovery client's devdata */
  1912. ops = recoveryClientCooler(cdev, &mcdata);
  1913. if (ops == NULL) {
  1914. THRML_ERROR_LOG("[.set_cur_state]E no cdev ops.\n");
  1915. mutex_unlock(&MTM_COOLER_LOCK);
  1916. return -1;
  1917. }
  1918. if (ops->get_cur_state)
  1919. ret = ops->get_cur_state(cdev, &cur_state);
  1920. /* check conditions */
  1921. #if MTK_THERMAL_MONITOR_CONDITIONAL_COOLING
  1922. if (0 != state) {
  1923. /* here check conditions for setting the cooler... */
  1924. if (MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS ==
  1925. _mtkthermal_check_cooler_conditions(mcdata)) {
  1926. /* pass */
  1927. } else {
  1928. THRML_LOG
  1929. ("[.set_cur_state]condition check failed tz_type:%s cdev_type:%s trip:%d state:%lu\n",
  1930. mcdata->tz->type, cdev->type, mcdata->trip, state);
  1931. state = 0;
  1932. }
  1933. }
  1934. if (0 == state) {
  1935. int last_temp = 0;
  1936. unsigned long trip_temp = 0;
  1937. struct thermal_zone_device_ops *tz_ops;
  1938. if ((0 < mcdata->exit_threshold) && (mcdata->tz != NULL)) {
  1939. /* if exit point is set and if this cooler is still bound... */
  1940. THRML_LOG("[.set_cur_state] cur_state:%lu\n", cur_state);
  1941. if (0 < cur_state) {
  1942. THRML_LOG("[.set_cur_state] tz:%p devdata:%p\n", mcdata->tz,
  1943. mcdata->tz->devdata);
  1944. if (mcdata->tz)
  1945. last_temp = mcdata->tz->temperature;
  1946. THRML_LOG("[.set_cur_state] last_temp:%d\n", last_temp);
  1947. tz_ops = getClientZoneOps(mcdata->tz);
  1948. if (!ops) {
  1949. THRML_ERROR_LOG("[.set_cur_state]E tz unregistered.\n");
  1950. /* BUG(); */
  1951. trip_temp = 120000;
  1952. } else {
  1953. if (tz_ops->get_trip_temp) {
  1954. tz_ops->get_trip_temp(mcdata->tz, mcdata->trip, &trip_temp);
  1955. THRML_LOG("[.set_cur_state] trip_temp:%ld\n", (long)trip_temp);
  1956. } else {
  1957. BUG();
  1958. }
  1959. }
  1960. if ((last_temp >= (int)trip_temp)
  1961. || (((int)trip_temp - last_temp) < mcdata->exit_threshold)) {
  1962. THRML_LOG
  1963. ("[.set_cur_state]not exit yet tz_type:%s cdev_type:%s trip:%d state:%lu\n",
  1964. mcdata->tz->type, cdev->type, mcdata->trip, state);
  1965. state = cur_state;
  1966. }
  1967. }
  1968. }
  1969. }
  1970. #endif
  1971. THRML_LOG("[.set_cur_state] tz_type:%s cdev_type:%s trip:%d state:%lu\n", mcdata->tz->type,
  1972. cdev->type, mcdata->trip, state);
  1973. THRML_STORAGE_LOG(THRML_LOGGER_MSG_COOL_STAE, set_cur_state, mcdata->tz->type, mcdata->trip,
  1974. cdev->type, state);
  1975. if (ops->set_cur_state)
  1976. ret = ops->set_cur_state(cdev, state);
  1977. /* reset devdata to mcdata */
  1978. cdev->devdata = mcdata;
  1979. mutex_unlock(&MTM_COOLER_LOCK);
  1980. return ret;
  1981. }
  1982. /* Cooling callbacks OPS */
  1983. static struct thermal_cooling_device_ops mtk_cooling_wrapper_dev_ops = {
  1984. .get_max_state = mtk_cooling_wrapper_get_max_state,
  1985. .get_cur_state = mtk_cooling_wrapper_get_cur_state,
  1986. .set_cur_state = mtk_cooling_wrapper_set_cur_state,
  1987. };
  1988. /*
  1989. * MTK Cooling Register
  1990. */
  1991. struct thermal_cooling_device *mtk_thermal_cooling_device_register_wrapper
  1992. (char *type, void *devdata, const struct thermal_cooling_device_ops *ops) {
  1993. struct mtk_thermal_cooler_data *mcdata = NULL;
  1994. struct thermal_cooling_device *ret = NULL;
  1995. int i = 0;
  1996. THRML_LOG("%s type:%s\n", __func__, type);
  1997. mcdata = kzalloc(sizeof(struct mtk_thermal_cooler_data), GFP_KERNEL);
  1998. if (!mcdata) {
  1999. THRML_ERROR_LOG("%s mcdata kzalloc fail.\n", __func__);
  2000. return ERR_PTR(-ENOMEM);
  2001. }
  2002. mcdata->ops = (struct thermal_cooling_device_ops *)ops;
  2003. mcdata->devdata = devdata;
  2004. mcdata->exit_threshold = 0;
  2005. for (; i < MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS; i++) {
  2006. mcdata->conditions[i][0] = 0x0;
  2007. mcdata->condition_last_value[i] = NULL;
  2008. mcdata->threshold[i] = 0;
  2009. }
  2010. mb();
  2011. /* create a proc for this cooler... */
  2012. if (NULL != _get_proc_cooler_dir_entry()) {
  2013. struct proc_dir_entry *entry;
  2014. entry =
  2015. proc_create_data((const char *)type, S_IRUGO | S_IWUSR | S_IWGRP,
  2016. proc_cooler_dir_entry, &_mtkthermal_cooler_fops, mcdata);
  2017. if (!entry) {
  2018. THRML_ERROR_LOG("%s proc file not created: %p\n", __func__, mcdata);
  2019. } else {
  2020. proc_set_user(entry, uid, gid);
  2021. THRML_LOG("%s proc file created: %p\n", __func__, mcdata);
  2022. }
  2023. }
  2024. ret = thermal_cooling_device_register(type, mcdata, &mtk_cooling_wrapper_dev_ops);
  2025. mcdata->id = ret->id; /* Used for CPU usage flag... */
  2026. return ret;
  2027. }
  2028. EXPORT_SYMBOL(mtk_thermal_cooling_device_register_wrapper);
  2029. /*
  2030. * MTK Cooling Unregister
  2031. */
  2032. void mtk_thermal_cooling_device_unregister_wrapper(struct thermal_cooling_device *cdev)
  2033. {
  2034. struct mtk_thermal_cooler_data *mcdata;
  2035. char type[32] = { 0 };
  2036. strncpy(type, cdev->type, 20);
  2037. THRML_LOG("%s+ cdev:%p devdata:%p cdev:%s\n", __func__, cdev, cdev->devdata, type);
  2038. /* delete the proc file entry from proc */
  2039. if (NULL != proc_cooler_dir_entry)
  2040. remove_proc_entry((const char *)type, proc_cooler_dir_entry);
  2041. /* TODO: consider error handling... */
  2042. mutex_lock(&MTM_COOLER_LOCK);
  2043. /* free mtk cooler data */
  2044. mcdata = cdev->devdata;
  2045. mutex_unlock(&MTM_COOLER_LOCK);
  2046. THRML_LOG("%s- mcdata:%p\n", __func__, mcdata);
  2047. thermal_cooling_device_unregister(cdev);
  2048. /* free mtk cooler data */
  2049. kfree(mcdata);
  2050. THRML_LOG("%s- cdev: %s\n", __func__, type);
  2051. }
  2052. EXPORT_SYMBOL(mtk_thermal_cooling_device_unregister_wrapper);
  2053. int mtk_thermal_zone_bind_trigger_trip(struct thermal_zone_device *tz, int trip, int mode)
  2054. {
  2055. THRML_LOG("%s trip %d\n", __func__, trip);
  2056. schedule_delayed_work(&(tz->poll_queue), 0);
  2057. return 0;
  2058. }
  2059. EXPORT_SYMBOL(mtk_thermal_zone_bind_trigger_trip);
  2060. int mtk_thermal_get_temp(MTK_THERMAL_SENSOR_ID id)
  2061. {
  2062. int ret = 0;
  2063. if (id < 0 || id >= MTK_THERMAL_SENSOR_COUNT)
  2064. return -127000;
  2065. mutex_lock(&MTM_GET_TEMP_LOCK);
  2066. if (tz_last_values[id] == NULL) {
  2067. mutex_unlock(&MTM_GET_TEMP_LOCK);
  2068. return -127000;
  2069. }
  2070. ret = *tz_last_values[id];
  2071. mutex_unlock(&MTM_GET_TEMP_LOCK);
  2072. return ret;
  2073. }
  2074. EXPORT_SYMBOL(mtk_thermal_get_temp);
  2075. struct proc_dir_entry *mtk_thermal_get_proc_drv_therm_dir_entry(void)
  2076. {
  2077. mutex_lock(&MTM_DRV_THERM_PROC_DIR_LOCK);
  2078. if (NULL == proc_drv_therm_dir_entry) {
  2079. proc_drv_therm_dir_entry = proc_mkdir("driver/thermal", NULL);
  2080. if (NULL == proc_drv_therm_dir_entry)
  2081. THRML_ERROR_LOG("[%s]: mkdir /proc/driver/thermal failed\n", __func__);
  2082. }
  2083. mutex_unlock(&MTM_DRV_THERM_PROC_DIR_LOCK);
  2084. return proc_drv_therm_dir_entry;
  2085. }
  2086. EXPORT_SYMBOL(mtk_thermal_get_proc_drv_therm_dir_entry);
  2087. module_init(mtkthermal_init);
  2088. module_exit(mtkthermal_exit);
  2089. #if defined(CONFIG_MTK_THERMAL_TIME_BASE_PROTECTION)
  2090. late_initcall(mtkthermal_late_init);
  2091. #endif