mtk_cooler_mutt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. #include <linux/version.h>
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #include <linux/types.h>
  5. #include <linux/kobject.h>
  6. #include <linux/proc_fs.h>
  7. #include <asm/uaccess.h>
  8. #include <linux/err.h>
  9. #include <linux/syscalls.h>
  10. #include "mt-plat/mtk_thermal_monitor.h"
  11. #include <mt_ccci_common.h>
  12. #include <linux/uidgid.h>
  13. #include <mt_cooler_setting.h>
  14. /* extern unsigned long ccci_get_md_boot_count(int md_id); */
  15. #if FEATURE_THERMAL_DIAG
  16. /* signal */
  17. /* #define MAX_LEN 256 */
  18. static unsigned int tmd_pid;
  19. static unsigned int tmd_input_pid;
  20. static struct task_struct tmd_task;
  21. static struct task_struct *ptmd_task = &tmd_task;
  22. #endif
  23. #if FEATURE_MUTT_V2
  24. /* signal */
  25. #define MAX_LEN 256
  26. static unsigned int tm_pid;
  27. static unsigned int tm_input_pid;
  28. static struct task_struct g_task;
  29. static struct task_struct *pg_task = &g_task;
  30. /* mdoff cooler */
  31. static struct thermal_cooling_device *cl_dev_mdoff;
  32. static unsigned int cl_dev_mdoff_state;
  33. /* noIMS cooler */
  34. static struct thermal_cooling_device *cl_dev_noIMS;
  35. static unsigned int cl_dev_noIMS_state;
  36. #endif
  37. unsigned long __attribute__ ((weak))
  38. ccci_get_md_boot_count(int md_id)
  39. {
  40. pr_err("E_WF: %s doesn't exist\n", __func__);
  41. return 0;
  42. }
  43. int __attribute__ ((weak))
  44. exec_ccci_kern_func_by_md_id(int md_id, unsigned int id, char *buf, unsigned int len)
  45. {
  46. pr_err("E_WF: %s doesn't exist\n", __func__);
  47. return -316;
  48. }
  49. #define mtk_cooler_mutt_dprintk_always(fmt, args...) \
  50. pr_debug("thermal/cooler/mutt" fmt, ##args)
  51. #define mtk_cooler_mutt_dprintk(fmt, args...) \
  52. do { \
  53. if (1 == cl_mutt_klog_on) \
  54. pr_debug("[thermal/cooler/mutt]" fmt, ##args); \
  55. } while (0)
  56. /* State of "MD off & noIMS" are not included. */
  57. #define MAX_NUM_INSTANCE_MTK_COOLER_MUTT 4
  58. #define MTK_CL_MUTT_GET_LIMIT(limit, state) \
  59. { (limit) = (short) (((unsigned long) (state))>>16); }
  60. #define MTK_CL_MUTT_SET_LIMIT(limit, state) \
  61. { state = ((((unsigned long) (state))&0xFFFF) | ((short) limit<<16)); }
  62. #define MTK_CL_MUTT_GET_CURR_STATE(curr_state, state) \
  63. { curr_state = (((unsigned long) (state))&0xFFFF); }
  64. #define MTK_CL_MUTT_SET_CURR_STATE(curr_state, state) \
  65. do { \
  66. if (0 == curr_state) \
  67. state &= ~0x1; \
  68. else \
  69. state |= 0x1; \
  70. } while (0)
  71. #if FEATURE_MUTT_V2
  72. /**
  73. * No UL data(except IMS): active = 1; suspend = 255; bit0 in reserved =0;
  74. * No UL data(no IMS): active = 1; suspend = 255; bit0 in reserved =1;
  75. */
  76. #define BIT_MD_CL_NO_IMS 0x01000000
  77. #define MD_CL_NO_UL_DATA 0x00FF0101
  78. #endif
  79. static kuid_t uid = KUIDT_INIT(0);
  80. static kgid_t gid = KGIDT_INIT(1000);
  81. static int cl_mutt_klog_on;
  82. static struct thermal_cooling_device *cl_mutt_dev[MAX_NUM_INSTANCE_MTK_COOLER_MUTT] = { 0 };
  83. static unsigned int cl_mutt_param[MAX_NUM_INSTANCE_MTK_COOLER_MUTT] = { 0 };
  84. static unsigned long cl_mutt_state[MAX_NUM_INSTANCE_MTK_COOLER_MUTT] = { 0 };
  85. static unsigned int cl_mutt_cur_limit;
  86. static unsigned long last_md_boot_cnt;
  87. #if FEATURE_THERMAL_DIAG
  88. /*
  89. * use "si_code" for Action identify
  90. * for tmd_pid (/system/bin/thermald)
  91. */
  92. enum {
  93. /* TMD_Alert_ShutDown = 1, */
  94. TMD_Alert_ULdataBack = 2,
  95. TMD_Alert_NOULdata = 3
  96. };
  97. static int clmutt_send_tmd_signal(int level)
  98. {
  99. int ret = 0;
  100. static int warning_state = TMD_Alert_ULdataBack;
  101. if (warning_state == level)
  102. return ret;
  103. if (tmd_input_pid == 0) {
  104. mtk_cooler_mutt_dprintk("%s pid is empty\n", __func__);
  105. ret = -1;
  106. }
  107. mtk_cooler_mutt_dprintk_always(" %s pid is %d, %d; MD_Alert: %d\n", __func__,
  108. tmd_pid, tmd_input_pid, level);
  109. if (ret == 0 && tmd_input_pid != tmd_pid) {
  110. tmd_pid = tmd_input_pid;
  111. ptmd_task = get_pid_task(find_vpid(tmd_pid), PIDTYPE_PID);
  112. }
  113. if (ret == 0 && ptmd_task) {
  114. siginfo_t info;
  115. info.si_signo = SIGIO;
  116. info.si_errno = 0;
  117. info.si_code = level;
  118. info.si_addr = NULL;
  119. ret = send_sig_info(SIGIO, &info, ptmd_task);
  120. }
  121. if (ret != 0)
  122. mtk_cooler_mutt_dprintk_always(" %s ret=%d\n", __func__, ret);
  123. else {
  124. if (TMD_Alert_ULdataBack == level)
  125. warning_state = TMD_Alert_ULdataBack;
  126. else if (TMD_Alert_NOULdata == level)
  127. warning_state = TMD_Alert_NOULdata;
  128. }
  129. return ret;
  130. }
  131. static ssize_t clmutt_tmd_pid_write(struct file *filp, const char __user *buf, size_t count,
  132. loff_t *data)
  133. {
  134. int ret = 0;
  135. char tmp[MAX_LEN] = { 0 };
  136. int len = 0;
  137. len = (count < (MAX_LEN - 1)) ? count : (MAX_LEN - 1);
  138. /* write data to the buffer */
  139. if (copy_from_user(tmp, buf, len))
  140. return -EFAULT;
  141. ret = kstrtouint(tmp, 10, &tmd_input_pid);
  142. if (ret)
  143. WARN_ON(1);
  144. mtk_cooler_mutt_dprintk("%s %s = %d\n", __func__, tmp, tmd_input_pid);
  145. return len;
  146. }
  147. static int clmutt_tmd_pid_read(struct seq_file *m, void *v)
  148. {
  149. seq_printf(m, "%d\n", tmd_input_pid);
  150. mtk_cooler_mutt_dprintk("%s %d\n", __func__, tmd_input_pid);
  151. return 0;
  152. }
  153. static int clmutt_tmd_pid_open(struct inode *inode, struct file *file)
  154. {
  155. return single_open(file, clmutt_tmd_pid_read, PDE_DATA(inode));
  156. }
  157. static const struct file_operations clmutt_tmd_pid_fops = {
  158. .owner = THIS_MODULE,
  159. .open = clmutt_tmd_pid_open,
  160. .read = seq_read,
  161. .llseek = seq_lseek,
  162. .write = clmutt_tmd_pid_write,
  163. .release = single_release,
  164. };
  165. #endif
  166. #if FEATURE_MUTT_V2
  167. /*
  168. * use "si_errno" for client identify
  169. * for tm_pid (/system/bin/thermal)
  170. */
  171. enum {
  172. /* TM_CLIENT_clwmt = 0,
  173. TM_CLIENT_mdulthro =1,
  174. TM_CLIENT_mddlthro =2, */
  175. TM_CLIENT_clmutt = 3
  176. };
  177. static int clmutt_send_tm_signal(int level)
  178. {
  179. int ret = 0, j = 0;
  180. if (cl_dev_mdoff_state == level)
  181. return ret;
  182. if (tm_input_pid == 0) {
  183. mtk_cooler_mutt_dprintk("[%s] pid is empty\n", __func__);
  184. ret = -1;
  185. }
  186. mtk_cooler_mutt_dprintk_always("[%s] pid is %d, %d; MD off: %d\n", __func__,
  187. tm_pid, tm_input_pid, level);
  188. if (ret == 0 && tm_input_pid != tm_pid) {
  189. tm_pid = tm_input_pid;
  190. pg_task = get_pid_task(find_vpid(tm_pid), PIDTYPE_PID);
  191. }
  192. if (ret == 0 && pg_task) {
  193. siginfo_t info;
  194. info.si_signo = SIGIO;
  195. info.si_errno = TM_CLIENT_clmutt;
  196. info.si_code = level; /* Toggle MD ON: 0 OFF: 1*/
  197. info.si_addr = NULL;
  198. ret = send_sig_info(SIGIO, &info, pg_task);
  199. }
  200. if (ret != 0)
  201. mtk_cooler_mutt_dprintk_always("[%s] ret=%d\n", __func__, ret);
  202. else {
  203. if (1 == level) {
  204. cl_dev_mdoff_state = level;
  205. cl_dev_noIMS_state = 0;
  206. cl_mutt_cur_limit = 0;
  207. for (; j < MAX_NUM_INSTANCE_MTK_COOLER_MUTT; j++)
  208. MTK_CL_MUTT_SET_CURR_STATE(0, cl_mutt_state[j]);
  209. } else
  210. cl_dev_mdoff_state = 0;
  211. }
  212. return ret;
  213. }
  214. static ssize_t clmutt_tm_pid_write(struct file *filp, const char __user *buf, size_t count, loff_t *data)
  215. {
  216. int ret = 0;
  217. char tmp[MAX_LEN] = {0};
  218. int len = 0;
  219. len = (count < (MAX_LEN - 1)) ? count : (MAX_LEN - 1);
  220. /* write data to the buffer */
  221. if (copy_from_user(tmp, buf, len))
  222. return -EFAULT;
  223. ret = kstrtouint(tmp, 10, &tm_input_pid);
  224. if (ret)
  225. WARN_ON(1);
  226. mtk_cooler_mutt_dprintk("[%s] %s = %d\n", __func__, tmp, tm_input_pid);
  227. return len;
  228. }
  229. static int clmutt_tm_pid_read(struct seq_file *m, void *v)
  230. {
  231. seq_printf(m, "%d\n", tm_input_pid);
  232. mtk_cooler_mutt_dprintk("[%s] %d\n", __func__, tm_input_pid);
  233. return 0;
  234. }
  235. static int clmutt_tm_pid_open(struct inode *inode, struct file *file)
  236. {
  237. return single_open(file, clmutt_tm_pid_read, PDE_DATA(inode));
  238. }
  239. static const struct file_operations clmutt_tm_pid_fops = {
  240. .owner = THIS_MODULE,
  241. .open = clmutt_tm_pid_open,
  242. .read = seq_read,
  243. .llseek = seq_lseek,
  244. .write = clmutt_tm_pid_write,
  245. .release = single_release,
  246. };
  247. /*
  248. * cooling device callback functions (mtk_cl_mdoff_ops)
  249. * 1 : True and 0 : False
  250. */
  251. static int mtk_cl_mdoff_get_max_state(struct thermal_cooling_device *cdev,
  252. unsigned long *state)
  253. {
  254. *state = 1;
  255. mtk_cooler_mutt_dprintk("mtk_cl_mdoff_get_max_state() %s %lu\n", cdev->type, *state);
  256. return 0;
  257. }
  258. static int mtk_cl_mdoff_get_cur_state(struct thermal_cooling_device *cdev,
  259. unsigned long *state)
  260. {
  261. *state = cl_dev_mdoff_state;
  262. mtk_cooler_mutt_dprintk("mtk_cl_mdoff_get_max_state() %s %lu (0: md on; 1: md off)\n", cdev->type, *state);
  263. return 0;
  264. }
  265. static int mtk_cl_mdoff_set_cur_state(struct thermal_cooling_device *cdev,
  266. unsigned long state)
  267. {
  268. if ((state >= 0) && (state <= 1))
  269. mtk_cooler_mutt_dprintk("mtk_cl_mdoff_set_cur_state() %s %lu (0: md on; 1: md off)\n",
  270. cdev->type, state);
  271. else {
  272. mtk_cooler_mutt_dprintk("mtk_cl_mdoff_set_cur_state(): Invalid input (0:md on; 1: md off)\n");
  273. return 0;
  274. }
  275. clmutt_send_tm_signal(state);
  276. return 0;
  277. }
  278. static struct thermal_cooling_device_ops mtk_cl_mdoff_ops = {
  279. .get_max_state = mtk_cl_mdoff_get_max_state,
  280. .get_cur_state = mtk_cl_mdoff_get_cur_state,
  281. .set_cur_state = mtk_cl_mdoff_set_cur_state,
  282. };
  283. static void mtk_cl_mutt_set_onIMS(int level)
  284. {
  285. int ret = 0;
  286. unsigned int cl_mutt_param_noIMS = 0;
  287. if (cl_dev_noIMS_state == level)
  288. return;
  289. if (level)
  290. cl_mutt_param_noIMS = (MD_CL_NO_UL_DATA | BIT_MD_CL_NO_IMS);
  291. else
  292. cl_mutt_param_noIMS = MD_CL_NO_UL_DATA;
  293. if (cl_mutt_param_noIMS != cl_mutt_cur_limit) {
  294. cl_mutt_cur_limit = cl_mutt_param_noIMS;
  295. last_md_boot_cnt = ccci_get_md_boot_count(MD_SYS1);
  296. ret = exec_ccci_kern_func_by_md_id(MD_SYS1, ID_THROTTLING_CFG, (char *) &cl_mutt_cur_limit, 4);
  297. mtk_cooler_mutt_dprintk_always("[%s] ret %d param %x bcnt %lul\n",
  298. __func__, ret, cl_mutt_cur_limit, last_md_boot_cnt);
  299. } else if (cl_mutt_param_noIMS != 0) {
  300. unsigned long cur_md_bcnt = ccci_get_md_boot_count(MD_SYS1);
  301. if (last_md_boot_cnt != cur_md_bcnt) {
  302. last_md_boot_cnt = cur_md_bcnt;
  303. ret = exec_ccci_kern_func_by_md_id(MD_SYS1, ID_THROTTLING_CFG, (char *) &cl_mutt_cur_limit, 4);
  304. mtk_cooler_mutt_dprintk_always("[%s] mdrb ret %d param %x bcnt %lul\n",
  305. __func__, ret, cl_mutt_cur_limit, last_md_boot_cnt);
  306. }
  307. }
  308. if (ret != 0)
  309. mtk_cooler_mutt_dprintk_always("[%s] ret=%d\n", __func__, ret);
  310. else {
  311. if (1 == level)
  312. cl_dev_noIMS_state = level;
  313. else
  314. cl_dev_noIMS_state = 0;
  315. }
  316. }
  317. /*
  318. * cooling device callback functions (mtk_cl_noIMS_ops)
  319. * 1 : True and 0 : False
  320. */
  321. static int mtk_cl_noIMS_get_max_state(struct thermal_cooling_device *cdev,
  322. unsigned long *state)
  323. {
  324. *state = 1;
  325. mtk_cooler_mutt_dprintk("mtk_cl_noIMS_get_max_state() %s %lu\n", cdev->type, *state);
  326. return 0;
  327. }
  328. static int mtk_cl_noIMS_get_cur_state(struct thermal_cooling_device *cdev,
  329. unsigned long *state)
  330. {
  331. *state = cl_dev_noIMS_state;
  332. mtk_cooler_mutt_dprintk("mtk_cl_noIMS_get_max_state() %s %lu (0: md IMS OK; 1: md no IMS)\n",
  333. cdev->type, *state);
  334. return 0;
  335. }
  336. static int mtk_cl_noIMS_set_cur_state(struct thermal_cooling_device *cdev,
  337. unsigned long state)
  338. {
  339. if (1 == cl_dev_mdoff_state) {
  340. mtk_cooler_mutt_dprintk("mtk_cl_noIMS_set_cur_state(): MD STILL OFF!!\n");
  341. return 0;
  342. }
  343. if ((state >= 0) && (state <= 1))
  344. mtk_cooler_mutt_dprintk("mtk_cl_noIMS_set_cur_state() %s %lu (0: md IMS OK; 1: md no IMS)\n",
  345. cdev->type, state);
  346. else {
  347. mtk_cooler_mutt_dprintk("mtk_cl_noIMS_set_cur_state(): Invalid input(0: md IMS OK; 1: md no IMS)\n");
  348. return 0;
  349. }
  350. mtk_cl_mutt_set_onIMS(state);
  351. return 0;
  352. }
  353. static struct thermal_cooling_device_ops mtk_cl_noIMS_ops = {
  354. .get_max_state = mtk_cl_noIMS_get_max_state,
  355. .get_cur_state = mtk_cl_noIMS_get_cur_state,
  356. .set_cur_state = mtk_cl_noIMS_set_cur_state,
  357. };
  358. #endif
  359. static void mtk_cl_mutt_set_mutt_limit(void)
  360. {
  361. /* TODO: optimize */
  362. int i = 0, j = 0, ret = 0;
  363. int min_limit = 255;
  364. unsigned int min_param = 0;
  365. for (; i < MAX_NUM_INSTANCE_MTK_COOLER_MUTT; i++) {
  366. unsigned long curr_state;
  367. MTK_CL_MUTT_GET_CURR_STATE(curr_state, cl_mutt_state[i]);
  368. if (1 == curr_state) {
  369. unsigned int active;
  370. unsigned int suspend;
  371. int limit = 0;
  372. active = (cl_mutt_param[i] & 0x0000FF00) >> 8;
  373. suspend = (cl_mutt_param[i] & 0x00FF0000) >> 16;
  374. /* a cooler with 0 active or 0 suspend is not allowed */
  375. if (active == 0 || suspend == 0)
  376. goto err_unreg;
  377. /* compare the active/suspend ratio */
  378. if (active >= suspend)
  379. limit = active / suspend;
  380. else
  381. limit = (0 - suspend) / active;
  382. if (limit <= min_limit) {
  383. min_limit = limit;
  384. min_param = cl_mutt_param[i];
  385. }
  386. }
  387. }
  388. if (min_param != cl_mutt_cur_limit) {
  389. cl_mutt_cur_limit = min_param;
  390. last_md_boot_cnt = ccci_get_md_boot_count(MD_SYS1);
  391. ret =
  392. exec_ccci_kern_func_by_md_id(MD_SYS1, ID_THROTTLING_CFG,
  393. (char *)&cl_mutt_cur_limit, 4);
  394. mtk_cooler_mutt_dprintk_always("[%s] ret %d param %x bcnt %lul\n", __func__, ret,
  395. cl_mutt_cur_limit, last_md_boot_cnt);
  396. } else if (min_param != 0) {
  397. unsigned long cur_md_bcnt = ccci_get_md_boot_count(MD_SYS1);
  398. if (last_md_boot_cnt != cur_md_bcnt) {
  399. last_md_boot_cnt = cur_md_bcnt;
  400. ret =
  401. exec_ccci_kern_func_by_md_id(MD_SYS1, ID_THROTTLING_CFG,
  402. (char *)&cl_mutt_cur_limit, 4);
  403. mtk_cooler_mutt_dprintk_always("[%s] mdrb ret %d param %x bcnt %lul\n",
  404. __func__, ret, cl_mutt_cur_limit,
  405. last_md_boot_cnt);
  406. }
  407. } else
  408. return;
  409. if (ret != 0) {
  410. cl_mutt_cur_limit = 0;
  411. for (; j < MAX_NUM_INSTANCE_MTK_COOLER_MUTT; j++)
  412. MTK_CL_MUTT_SET_CURR_STATE(0, cl_mutt_state[j]);
  413. mtk_cooler_mutt_dprintk_always("[%s] ret=%d\n", __func__, ret);
  414. }
  415. #if FEATURE_THERMAL_DIAG
  416. else {
  417. if (cl_mutt_cur_limit == MD_CL_NO_UL_DATA)
  418. clmutt_send_tmd_signal(TMD_Alert_NOULdata);
  419. else
  420. clmutt_send_tmd_signal(TMD_Alert_ULdataBack);
  421. }
  422. #endif
  423. err_unreg:
  424. return;
  425. }
  426. static int mtk_cl_mutt_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
  427. {
  428. *state = 1;
  429. mtk_cooler_mutt_dprintk("mtk_cl_mutt_get_max_state() %s %lu\n", cdev->type, *state);
  430. return 0;
  431. }
  432. static int mtk_cl_mutt_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
  433. {
  434. MTK_CL_MUTT_GET_CURR_STATE(*state, *((unsigned long *)cdev->devdata));
  435. mtk_cooler_mutt_dprintk("mtk_cl_mutt_get_cur_state() %s %lu\n", cdev->type, *state);
  436. return 0;
  437. }
  438. static int mtk_cl_mutt_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  439. {
  440. #if FEATURE_MUTT_V2
  441. if ((1 == cl_dev_mdoff_state) || (1 == cl_dev_noIMS_state)) {
  442. mtk_cooler_mutt_dprintk("mtk_cl_mutt_set_cur_state(): MD OFF or noIMS!!\n");
  443. return 0;
  444. }
  445. #endif
  446. mtk_cooler_mutt_dprintk("mtk_cl_mutt_set_cur_state() %s %lu\n", cdev->type, state);
  447. MTK_CL_MUTT_SET_CURR_STATE(state, *((unsigned long *)cdev->devdata));
  448. mtk_cl_mutt_set_mutt_limit();
  449. return 0;
  450. }
  451. /* bind fan callbacks to fan device */
  452. static struct thermal_cooling_device_ops mtk_cl_mutt_ops = {
  453. .get_max_state = mtk_cl_mutt_get_max_state,
  454. .get_cur_state = mtk_cl_mutt_get_cur_state,
  455. .set_cur_state = mtk_cl_mutt_set_cur_state,
  456. };
  457. static int mtk_cooler_mutt_register_ltf(void)
  458. {
  459. int i;
  460. mtk_cooler_mutt_dprintk("register ltf\n");
  461. for (i = MAX_NUM_INSTANCE_MTK_COOLER_MUTT; i-- > 0;) {
  462. char temp[20] = { 0 };
  463. sprintf(temp, "mtk-cl-mutt%02d", i);
  464. /* put mutt state to cooler devdata */
  465. cl_mutt_dev[i] = mtk_thermal_cooling_device_register(temp, (void *)&cl_mutt_state[i],
  466. &mtk_cl_mutt_ops);
  467. }
  468. #if FEATURE_MUTT_V2
  469. cl_dev_noIMS = mtk_thermal_cooling_device_register("mtk-cl-noIMS", NULL,
  470. &mtk_cl_noIMS_ops);
  471. cl_dev_mdoff = mtk_thermal_cooling_device_register("mtk-cl-mdoff", NULL,
  472. &mtk_cl_mdoff_ops);
  473. #endif
  474. return 0;
  475. }
  476. static void mtk_cooler_mutt_unregister_ltf(void)
  477. {
  478. int i;
  479. mtk_cooler_mutt_dprintk("unregister ltf\n");
  480. for (i = MAX_NUM_INSTANCE_MTK_COOLER_MUTT; i-- > 0;) {
  481. if (cl_mutt_dev[i]) {
  482. mtk_thermal_cooling_device_unregister(cl_mutt_dev[i]);
  483. cl_mutt_dev[i] = NULL;
  484. cl_mutt_state[i] = 0;
  485. }
  486. }
  487. #if FEATURE_MUTT_V2
  488. if (cl_dev_noIMS) {
  489. mtk_thermal_cooling_device_unregister(cl_dev_noIMS);
  490. cl_dev_noIMS = NULL;
  491. }
  492. if (cl_dev_mdoff) {
  493. mtk_thermal_cooling_device_unregister(cl_dev_mdoff);
  494. cl_dev_mdoff = NULL;
  495. }
  496. #endif
  497. }
  498. static int _mtk_cl_mutt_proc_read(struct seq_file *m, void *v)
  499. {
  500. /**
  501. * The format to print out:
  502. * kernel_log <0 or 1>
  503. * <mtk-cl-mutt<ID>> <active (ms)> <suspend (ms)> <param> <state>
  504. * ..
  505. */
  506. {
  507. int i = 0;
  508. seq_printf(m, "klog %d\n", cl_mutt_klog_on);
  509. #if FEATURE_MUTT_V2
  510. seq_printf(m, "curr_limit %x, noIMS: %d, mdoff: %d\n", cl_mutt_cur_limit,
  511. cl_dev_noIMS_state, cl_dev_mdoff_state);
  512. #else
  513. seq_printf(m, "curr_limit %x\n", cl_mutt_cur_limit);
  514. #endif
  515. for (; i < MAX_NUM_INSTANCE_MTK_COOLER_MUTT; i++) {
  516. unsigned int active;
  517. unsigned int suspend;
  518. unsigned long curr_state;
  519. active = (cl_mutt_param[i] & 0x0000FF00) >> 8;
  520. suspend = (cl_mutt_param[i] & 0x00FF0000) >> 16;
  521. MTK_CL_MUTT_GET_CURR_STATE(curr_state, cl_mutt_state[i]);
  522. seq_printf(m, "mtk-cl-mutt%02d %u %u %x, state %lu\n", i, active, suspend,
  523. cl_mutt_param[i], curr_state);
  524. }
  525. }
  526. return 0;
  527. }
  528. static ssize_t _mtk_cl_mutt_proc_write(struct file *filp, const char __user *buffer, size_t count,
  529. loff_t *data)
  530. {
  531. int len = 0;
  532. char desc[128];
  533. int klog_on, mutt0_a, mutt0_s, mutt1_a, mutt1_s, mutt2_a, mutt2_s, mutt3_a, mutt3_s;
  534. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  535. if (copy_from_user(desc, buffer, len))
  536. return 0;
  537. desc[len] = '\0';
  538. /**
  539. * sscanf format <klog_on> <mtk-cl-mutt00 active (ms)> <mtk-cl-mutt00 suspended (ms)> <mtk-cl-mutt01 active (ms)> <mtk-cl-mutt01 suspended (ms)> <mtk-cl-mutt02 active (ms)> <mtk-cl-mutt02 suspended (ms)>...
  540. * <klog_on> can only be 0 or 1
  541. * <mtk-cl-mutt* active/suspended (ms) > can only be positive integer or 0 to denote no limit
  542. */
  543. if (NULL == data) {
  544. mtk_cooler_mutt_dprintk("[%s] null data\n", __func__);
  545. return -EINVAL;
  546. }
  547. /* WARNING: Modify here if MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS is changed to other than 4 */
  548. #if (4 == MAX_NUM_INSTANCE_MTK_COOLER_MUTT)
  549. /* cl_mutt_param[0] = 0; */
  550. /* cl_mutt_param[1] = 0; */
  551. /* cl_mutt_param[2] = 0; */
  552. if (1 <= sscanf(desc, "%d %d %d %d %d %d %d %d %d",
  553. &klog_on, &mutt0_a, &mutt0_s, &mutt1_a, &mutt1_s, &mutt2_a, &mutt2_s,
  554. &mutt3_a, &mutt3_s)) {
  555. if (klog_on == 0 || klog_on == 1)
  556. cl_mutt_klog_on = klog_on;
  557. if (mutt0_a == 0)
  558. cl_mutt_param[0] = 0;
  559. else if (mutt0_a >= 100 && mutt0_a <= 25500 && mutt0_s >= 100 && mutt0_s <= 25500)
  560. cl_mutt_param[0] = ((mutt0_s / 100) << 16) | ((mutt0_a / 100) << 8) | 1;
  561. if (mutt1_a == 0)
  562. cl_mutt_param[1] = 0;
  563. else if (mutt1_a >= 100 && mutt1_a <= 25500 && mutt1_s >= 100 && mutt1_s <= 25500)
  564. cl_mutt_param[1] = ((mutt1_s / 100) << 16) | ((mutt1_a / 100) << 8) | 1;
  565. if (mutt2_a == 0)
  566. cl_mutt_param[2] = 0;
  567. else if (mutt2_a >= 100 && mutt2_a <= 25500 && mutt2_s >= 100 && mutt2_s <= 25500)
  568. cl_mutt_param[2] = ((mutt2_s / 100) << 16) | ((mutt2_a / 100) << 8) | 1;
  569. if (mutt3_a == 0)
  570. cl_mutt_param[3] = 0;
  571. else if (mutt3_a >= 100 && mutt3_a <= 25500 && mutt3_s >= 100 && mutt3_s <= 25500)
  572. cl_mutt_param[3] = ((mutt3_s / 100) << 16) | ((mutt3_a / 100) << 8) | 1;
  573. return count;
  574. }
  575. #else
  576. #error "Change correspondent part when changing MAX_NUM_INSTANCE_MTK_COOLER_MUTT!"
  577. #endif
  578. mtk_cooler_mutt_dprintk("[%s] bad arg\n", __func__);
  579. return -EINVAL;
  580. }
  581. static int _mtk_cl_mutt_proc_open(struct inode *inode, struct file *file)
  582. {
  583. return single_open(file, _mtk_cl_mutt_proc_read, NULL);
  584. }
  585. static const struct file_operations cl_mutt_fops = {
  586. .owner = THIS_MODULE,
  587. .open = _mtk_cl_mutt_proc_open,
  588. .read = seq_read,
  589. .llseek = seq_lseek,
  590. .write = _mtk_cl_mutt_proc_write,
  591. .release = single_release,
  592. };
  593. static int __init mtk_cooler_mutt_init(void)
  594. {
  595. int err = 0;
  596. int i;
  597. for (i = MAX_NUM_INSTANCE_MTK_COOLER_MUTT; i-- > 0;) {
  598. cl_mutt_dev[i] = NULL;
  599. cl_mutt_state[i] = 0;
  600. }
  601. mtk_cooler_mutt_dprintk("init\n");
  602. err = mtk_cooler_mutt_register_ltf();
  603. if (err)
  604. goto err_unreg;
  605. /* create a proc file */
  606. {
  607. struct proc_dir_entry *entry = NULL;
  608. struct proc_dir_entry *dir_entry = NULL;
  609. dir_entry = mtk_thermal_get_proc_drv_therm_dir_entry();
  610. if (!dir_entry) {
  611. mtk_cooler_mutt_dprintk_always("[%s]: mkdir /proc/driver/thermal failed\n",
  612. __func__);
  613. } else {
  614. entry =
  615. proc_create("clmutt", S_IRUGO | S_IWUSR | S_IWGRP, dir_entry,
  616. &cl_mutt_fops);
  617. if (entry)
  618. proc_set_user(entry, uid, gid);
  619. #if FEATURE_MUTT_V2
  620. entry = proc_create("clmutt_tm_pid", S_IRUGO | S_IWUSR | S_IWGRP,
  621. dir_entry, &clmutt_tm_pid_fops);
  622. if (entry)
  623. proc_set_user(entry, uid, gid);
  624. #endif
  625. #if FEATURE_THERMAL_DIAG
  626. entry = proc_create("clmutt_tmd_pid", S_IRUGO | S_IWUSR | S_IWGRP,
  627. dir_entry, &clmutt_tmd_pid_fops);
  628. if (entry)
  629. proc_set_user(entry, uid, gid);
  630. #endif
  631. }
  632. }
  633. return 0;
  634. err_unreg:
  635. mtk_cooler_mutt_unregister_ltf();
  636. return err;
  637. }
  638. static void __exit mtk_cooler_mutt_exit(void)
  639. {
  640. mtk_cooler_mutt_dprintk("exit\n");
  641. /* remove the proc file */
  642. remove_proc_entry("clmutt", NULL);
  643. mtk_cooler_mutt_unregister_ltf();
  644. }
  645. module_init(mtk_cooler_mutt_init);
  646. module_exit(mtk_cooler_mutt_exit);