mtk_cooler_fps.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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 <linux/slab.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/mtk_gpu_utility.h>
  13. #include "mt-plat/mtk_thermal_monitor.h"
  14. /* fps update from display */
  15. #include "disp_session.h"
  16. /* switch device to sent the (fps limit)uevent */
  17. #include <linux/switch.h>
  18. #include "mach/mt_thermal.h"
  19. #include <linux/uidgid.h>
  20. /* 1: turn on adaptive fps cooler; 0: turn off */
  21. #define ADAPTIVE_FPS_COOLER (1)
  22. #define mtk_cooler_fps_dprintk_always(fmt, args...) \
  23. pr_debug("thermal/cooler/fps" fmt, ##args)
  24. #define mtk_cooler_fps_dprintk(fmt, args...) \
  25. do { \
  26. if (1 == cl_fps_klog_on) \
  27. pr_debug("thermal/cooler/fps" fmt, ##args); \
  28. } while (0)
  29. #define MAX_NUM_INSTANCE_MTK_COOLER_FPS 4
  30. #define MTK_CL_FPS_GET_CURR_STATE(curr_state, state) \
  31. { curr_state = (((unsigned long) (state))&0xFFFF); }
  32. #define MTK_CL_FPS_SET_CURR_STATE(curr_state, state) \
  33. do { \
  34. if (0 == curr_state) \
  35. state &= ~0x1; \
  36. else \
  37. state |= 0x1; \
  38. } while (0)
  39. static kuid_t uid = KUIDT_INIT(0);
  40. static kgid_t gid = KGIDT_INIT(1000);
  41. static int cl_fps_klog_on;
  42. static struct thermal_cooling_device *cl_fps_dev[MAX_NUM_INSTANCE_MTK_COOLER_FPS] = { 0 };
  43. static unsigned int cl_fps_param[MAX_NUM_INSTANCE_MTK_COOLER_FPS] = { 0 };
  44. static unsigned long cl_fps_state[MAX_NUM_INSTANCE_MTK_COOLER_FPS] = { 0 };
  45. static unsigned int cl_fps_cur_limit;
  46. static unsigned int tm_input_fps;
  47. static struct switch_dev fps_switch_data;
  48. #if ADAPTIVE_FPS_COOLER
  49. /* TODO: TBD */
  50. #define MAX_FPS_LIMIT 60
  51. #define MIN_FPS_LIMIT 10
  52. #define MAX_FPS_LEVELS (MAX_FPS_LIMIT - MIN_FPS_LIMIT)
  53. #define DEFAULT_FPS_LEVEL 10
  54. static int fps_level[MAX_FPS_LEVELS];
  55. static int nr_fps_levels = MAX_FPS_LEVELS;
  56. static int curr_fps_level;
  57. #define MAX_FPS_SMA_LEN 10
  58. static int fps_history[MAX_FPS_SMA_LEN] = {0};
  59. static int fps_history_idx;
  60. static int fps_sma_len = MAX_FPS_SMA_LEN;
  61. #define MAX_TPCB_SMA_LEN 10
  62. static int tpcb_history[MAX_TPCB_SMA_LEN] = {0};
  63. static int tpcb_history_idx;
  64. static int tpcb_sma_len = MAX_TPCB_SMA_LEN;
  65. #define MAX_GPU_LOADING_SMA_LEN 10
  66. static int gpu_loading_history[MAX_GPU_LOADING_SMA_LEN] = {0};
  67. static int gpu_loading_history_idx;
  68. static int gpu_loading_sma_len = MAX_GPU_LOADING_SMA_LEN;
  69. static struct thermal_cooling_device *cl_adp_fps_dev;
  70. static unsigned int cl_adp_fps_state;
  71. static int cl_adp_fps_limit = MAX_FPS_LIMIT;
  72. #define GPU_LOADING_THRESHOLD 80
  73. /* in percentage */
  74. static int gpu_loading_threshold = GPU_LOADING_THRESHOLD;
  75. /* in percentage */
  76. static int fps_error_threshold = 10;
  77. /* in round */
  78. static int fps_stable_period = 10;
  79. /* FPS is active when over stable tpcb or always */
  80. static int fps_limit_always_on;
  81. static int leave_fps_limit_duration = 1;
  82. /* minimum fps that we regard as still in game playing */
  83. static int in_game_low_fps = 5;
  84. /* FIXME: need someone set/clear this */
  85. static int in_game_whitelist = 1;
  86. #endif
  87. static int fps_update(void)
  88. {
  89. disp_session_info info;
  90. memset(&info, 0, sizeof(info));
  91. info.session_id = MAKE_DISP_SESSION(DISP_SESSION_PRIMARY, 0);
  92. /* disp_mgr_get_session_info(&info); */ /* fix it */
  93. /* mtk_cooler_fps_dprintk("display update fps is: %d.%d\n", info.updateFPS/100, info.updateFPS%100); */
  94. /* mtk_cooler_fps_dprintk("is display fps stable: %d\n", info.is_updateFPS_stable); */
  95. #if 0
  96. if (info.is_updateFPS_stable)
  97. tm_input_fps = info.updateFPS;
  98. else
  99. tm_input_fps = 0;
  100. #else
  101. tm_input_fps = info.updateFPS;
  102. #endif
  103. return 0;
  104. }
  105. static void mtk_cl_fps_set_fps_limit(void)
  106. {
  107. int i = 0;
  108. int min_limit = 60;
  109. unsigned int min_param = 60;
  110. for (; i < MAX_NUM_INSTANCE_MTK_COOLER_FPS; i++) {
  111. unsigned long curr_state;
  112. MTK_CL_FPS_GET_CURR_STATE(curr_state, cl_fps_state[i]);
  113. if (1 == curr_state) {
  114. int limit = 0;
  115. limit = cl_fps_param[i];
  116. /* a cooler with 0 fps is not allowed */
  117. if (limit == 0)
  118. goto err_unreg;
  119. if (limit <= min_limit) {
  120. min_limit = limit;
  121. min_param = cl_fps_param[i];
  122. }
  123. }
  124. }
  125. #if ADAPTIVE_FPS_COOLER
  126. if (cl_adp_fps_limit < min_param)
  127. min_param = cl_adp_fps_limit;
  128. #endif
  129. if (min_param != cl_fps_cur_limit) {
  130. cl_fps_cur_limit = min_param;
  131. switch_set_state(&fps_switch_data, cl_fps_cur_limit);
  132. mtk_cooler_fps_dprintk_always("[%s] fps limit: %d\n", __func__, cl_fps_cur_limit);
  133. }
  134. err_unreg:
  135. return;
  136. }
  137. static int mtk_cl_fps_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
  138. {
  139. *state = 1;
  140. mtk_cooler_fps_dprintk("[%s] %s %lu\n", __func__, cdev->type, *state);
  141. return 0;
  142. }
  143. static int mtk_cl_fps_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
  144. {
  145. MTK_CL_FPS_GET_CURR_STATE(*state, *((unsigned long *) cdev->devdata));
  146. mtk_cooler_fps_dprintk("[%s] %s %lu\n", __func__, cdev->type, *state);
  147. return 0;
  148. }
  149. static int mtk_cl_fps_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  150. {
  151. mtk_cooler_fps_dprintk("[%s] %s %lu\n", __func__, cdev->type, state);
  152. MTK_CL_FPS_SET_CURR_STATE(state, *((unsigned long *) cdev->devdata));
  153. mtk_cl_fps_set_fps_limit();
  154. return 0;
  155. }
  156. #if ADAPTIVE_FPS_COOLER
  157. static int adp_fps_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
  158. {
  159. *state = 1;
  160. mtk_cooler_fps_dprintk("[%s] %s %lu\n", __func__, cdev->type, *state);
  161. return 0;
  162. }
  163. static int adp_fps_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
  164. {
  165. *state = cl_adp_fps_state;
  166. mtk_cooler_fps_dprintk("[%s] %s %lu\n", __func__, cdev->type, *state);
  167. return 0;
  168. }
  169. /* for gpu_loading, tpcb, fps */
  170. static int get_sma_val(int vals[], int sma_len)
  171. {
  172. int i, v = 0;
  173. for (i = 0; i < sma_len; i++)
  174. v += vals[i];
  175. v = v / sma_len;
  176. return v;
  177. }
  178. static void set_sma_val(int vals[], int sma_len, int *idx, int val)
  179. {
  180. vals[*idx] = val;
  181. *idx = (*idx + 1) % sma_len;
  182. }
  183. /* increase by one level */
  184. static int increase_fps_limit(void)
  185. {
  186. if (curr_fps_level > 0)
  187. curr_fps_level--;
  188. return fps_level[curr_fps_level];
  189. }
  190. /* decrease by one level */
  191. static int decrease_fps_limit(void)
  192. {
  193. if (curr_fps_level < (nr_fps_levels - 1))
  194. curr_fps_level++;
  195. return fps_level[curr_fps_level];
  196. }
  197. static int unlimit_fps_limit(void)
  198. {
  199. curr_fps_level = 0;
  200. return fps_level[curr_fps_level];
  201. }
  202. /* This function is actually an governor */
  203. static int adp_calc_fps_limit(void)
  204. {
  205. static int last_change_tpcb;
  206. static int period;
  207. int sma_tpcb, tpcb_change, sma_fps, gpu_loading;
  208. int fps_limit = fps_level[curr_fps_level];
  209. mtk_cooler_fps_dprintk("[%s] enter. period=%d, fps_stable_period=%d, fps_limit=%d\n",
  210. __func__, period, fps_stable_period, fps_limit);
  211. if (period < fps_stable_period) {
  212. period++;
  213. return fps_limit;
  214. }
  215. period = 0;
  216. gpu_loading = get_sma_val(gpu_loading_history, gpu_loading_sma_len);
  217. sma_tpcb = get_sma_val(tpcb_history, tpcb_sma_len);
  218. tpcb_change = sma_tpcb - last_change_tpcb;
  219. mtk_cooler_fps_dprintk("[%s] enter. gpu_loading=%d, sma_tpcb=%d, tpcb_change=%d\n",
  220. __func__, gpu_loading, sma_tpcb, tpcb_change);
  221. if (fps_limit_always_on || sma_tpcb >= mtk_thermal_get_tpcb_target()) {
  222. sma_fps = get_sma_val(fps_history, fps_sma_len);
  223. /* If gpu already utilizes almost full capacity but cannot reach the limit,
  224. * then we consider to decrease fps limit to avoid unstable fps */
  225. if (gpu_loading >= gpu_loading_threshold &&
  226. fps_limit - sma_fps >= fps_limit * fps_error_threshold / 100) {
  227. /* we do not limit FPS if not in game */
  228. if (in_game_whitelist)
  229. fps_limit = decrease_fps_limit();
  230. #if 0
  231. else {
  232. /* FIXME: give hint to somebody, so that user
  233. * can decides whether he/she wants fps to be
  234. * limited or not */
  235. /* send_hint_to_user(); */
  236. }
  237. #endif
  238. }
  239. }
  240. /* tpcb is falling and gpu loading is low, too */
  241. if (sma_tpcb < mtk_thermal_get_tpcb_target() && tpcb_change < 0
  242. && gpu_loading < gpu_loading_threshold) {
  243. fps_limit = increase_fps_limit();
  244. }
  245. if (tpcb_change)
  246. last_change_tpcb = sma_tpcb;
  247. /* mtk_cooler_fps_dprintk("[%s] enter. sma_fps=%d, fps_limit=%d\n",
  248. __func__, sma_fps, fps_limit); */
  249. return fps_limit;
  250. }
  251. static bool in_consistent_scene(void)
  252. {
  253. static int duration;
  254. int fps = tm_input_fps;
  255. if (!in_game_whitelist)
  256. return false;
  257. if (fps <= in_game_low_fps)
  258. duration++;
  259. else /* TODO: TBD: should we reset duration or decrease */
  260. duration = 0;
  261. if (duration >= leave_fps_limit_duration) {
  262. duration = 0;
  263. return false;
  264. } else
  265. return true;
  266. }
  267. static int adp_fps_set_cur_state(struct thermal_cooling_device *cdev,
  268. unsigned long state)
  269. {
  270. int gpu_loading;
  271. if ((state != 0) && (state != 1)) {
  272. mtk_cooler_fps_dprintk("[%s] invalid input (0: no thro; 1: adp fps thro on)\n", __func__);
  273. return 0;
  274. }
  275. mtk_cooler_fps_dprintk("[%s] %s %lu\n", __func__, cdev->type, state);
  276. cl_adp_fps_state = state;
  277. /* check the fps update from display */
  278. fps_update();
  279. set_sma_val(fps_history, fps_sma_len, &fps_history_idx, tm_input_fps);
  280. set_sma_val(tpcb_history, tpcb_sma_len, &tpcb_history_idx,
  281. mtk_thermal_get_temp(MTK_THERMAL_SENSOR_AP));
  282. if (!mtk_get_gpu_loading(&gpu_loading))
  283. gpu_loading = 0;
  284. set_sma_val(gpu_loading_history, gpu_loading_sma_len, &gpu_loading_history_idx,
  285. gpu_loading);
  286. /* 1. update the parameter of "cl_adp_fps_limit" */
  287. /* do we already leave game? */
  288. if (!in_consistent_scene())
  289. unlimit_fps_limit();
  290. cl_adp_fps_limit = adp_calc_fps_limit();
  291. /* 2. set the the limit */
  292. mtk_cl_fps_set_fps_limit();
  293. return 0;
  294. }
  295. static struct thermal_cooling_device_ops mtk_cl_adp_fps_ops = {
  296. .get_max_state = adp_fps_get_max_state,
  297. .get_cur_state = adp_fps_get_cur_state,
  298. .set_cur_state = adp_fps_set_cur_state,
  299. };
  300. static void reset_fps_level(void)
  301. {
  302. int i, fps;
  303. for (i = 0, fps = MAX_FPS_LIMIT;
  304. fps >= MIN_FPS_LIMIT && i < MAX_FPS_LEVELS;
  305. i++, fps -= DEFAULT_FPS_LEVEL)
  306. fps_level[i] = fps;
  307. nr_fps_levels = i;
  308. }
  309. static int clfps_level_read(struct seq_file *m, void *v)
  310. {
  311. int i;
  312. seq_printf(m, "%d ", nr_fps_levels);
  313. for (i = 0; i < nr_fps_levels; i++)
  314. seq_printf(m, "%d ", fps_level[i]);
  315. seq_puts(m, "\n");
  316. return 0;
  317. }
  318. static ssize_t clfps_level_write(struct file *file, const char __user *buffer,
  319. size_t count, loff_t *data)
  320. {
  321. char *buf, *ori_buf;
  322. unsigned int _tmp;
  323. int i, ret = -EINVAL;
  324. buf = kmalloc(count + 1, GFP_KERNEL);
  325. if (buf == NULL)
  326. return -EFAULT;
  327. /* buf would be modified in strsep() later */
  328. ori_buf = buf;
  329. if (copy_from_user(buf, buffer, count)) {
  330. ret = -EFAULT;
  331. goto exit;
  332. }
  333. buf[count] = '\0';
  334. if (kstrtoint(buf, 10, &_tmp) == 0 || _tmp > MAX_FPS_LEVELS) {
  335. ret = -EINVAL;
  336. goto exit;
  337. }
  338. nr_fps_levels = _tmp;
  339. for (i = 0; i < nr_fps_levels; i++) {
  340. strsep(&buf, " ");
  341. if (buf == NULL || kstrtoint(buf, 10, &_tmp) == 0 ||
  342. _tmp > MAX_FPS_LIMIT || _tmp < MIN_FPS_LIMIT) {
  343. ret = -EINVAL;
  344. goto exit;
  345. }
  346. fps_level[i] = _tmp;
  347. }
  348. ret = count;
  349. exit:
  350. kfree(ori_buf);
  351. if (ret < 0)
  352. reset_fps_level();
  353. return ret;
  354. }
  355. static int clfps_level_open(struct inode *inode, struct file *file)
  356. {
  357. return single_open(file, clfps_level_read, NULL);
  358. }
  359. static const struct file_operations clfps_level_fops = {
  360. .owner = THIS_MODULE,
  361. .open = clfps_level_open,
  362. .read = seq_read,
  363. .llseek = seq_lseek,
  364. .write = clfps_level_write,
  365. .release = single_release,
  366. };
  367. static int clfps_adp_read(struct seq_file *m, void *v)
  368. {
  369. seq_printf(m, "%d %d\n", MIN_FPS_LIMIT, MAX_FPS_LIMIT);
  370. return 0;
  371. }
  372. static ssize_t clfps_adp_write(struct file *file, const char __user *buffer,
  373. size_t count, loff_t *data)
  374. {
  375. char *buf;
  376. int _k_tt, _k_sum_tt, _min, _max;
  377. int ret = -EINVAL;
  378. buf = kmalloc(count + 1, GFP_KERNEL);
  379. if (buf == NULL)
  380. return -EFAULT;
  381. if (copy_from_user(buf, buffer, count)) {
  382. ret = -EFAULT;
  383. goto exit;
  384. }
  385. buf[count] = '\0';
  386. if (sscanf(buf, "%d %d %d %d", &_k_tt, &_k_sum_tt, &_min, &_max) == 5) {
  387. /* TODO: check the values are valid */
  388. ret = count;
  389. }
  390. exit:
  391. kfree(buf);
  392. return ret;
  393. }
  394. static int clfps_adp_open(struct inode *inode, struct file *file)
  395. {
  396. return single_open(file, clfps_adp_read, NULL);
  397. }
  398. static const struct file_operations clfps_adp_fops = {
  399. .owner = THIS_MODULE,
  400. .open = clfps_adp_open,
  401. .read = seq_read,
  402. .llseek = seq_lseek,
  403. .write = clfps_adp_write,
  404. .release = single_release,
  405. };
  406. #endif
  407. /* bind fan callbacks to fan device */
  408. static struct thermal_cooling_device_ops mtk_cl_fps_ops = {
  409. .get_max_state = mtk_cl_fps_get_max_state,
  410. .get_cur_state = mtk_cl_fps_get_cur_state,
  411. .set_cur_state = mtk_cl_fps_set_cur_state,
  412. };
  413. static int mtk_cooler_fps_register_ltf(void)
  414. {
  415. int i;
  416. mtk_cooler_fps_dprintk("register ltf\n");
  417. for (i = MAX_NUM_INSTANCE_MTK_COOLER_FPS; i-- > 0; ) {
  418. char temp[20] = { 0 };
  419. sprintf(temp, "mtk-cl-fps%02d", i);
  420. /* put fps state to cooler devdata */
  421. cl_fps_dev[i] = mtk_thermal_cooling_device_register(temp, (void *) &cl_fps_state[i],
  422. &mtk_cl_fps_ops);
  423. }
  424. #if ADAPTIVE_FPS_COOLER
  425. cl_adp_fps_dev = mtk_thermal_cooling_device_register("mtk-cl-adp-fps", NULL,
  426. &mtk_cl_adp_fps_ops);
  427. #endif
  428. return 0;
  429. }
  430. static void mtk_cooler_fps_unregister_ltf(void)
  431. {
  432. int i;
  433. mtk_cooler_fps_dprintk("unregister ltf\n");
  434. for (i = MAX_NUM_INSTANCE_MTK_COOLER_FPS; i-- > 0; ) {
  435. if (cl_fps_dev[i]) {
  436. mtk_thermal_cooling_device_unregister(cl_fps_dev[i]);
  437. cl_fps_dev[i] = NULL;
  438. cl_fps_state[i] = 0;
  439. }
  440. }
  441. #if ADAPTIVE_FPS_COOLER
  442. if (cl_adp_fps_dev) {
  443. mtk_thermal_cooling_device_unregister(cl_adp_fps_dev);
  444. cl_adp_fps_dev = NULL;
  445. cl_adp_fps_state = 0;
  446. }
  447. #endif
  448. }
  449. static int mtk_cl_fps_proc_read(struct seq_file *m, void *v)
  450. {
  451. /**
  452. * The format to print out:
  453. * kernel_log <0 or 1>
  454. * <mtk-cl-fps<ID>> <limited fps> <param> <state>
  455. * ..
  456. */
  457. {
  458. int i = 0;
  459. seq_printf(m, "klog %d\n", cl_fps_klog_on);
  460. seq_printf(m, "curr_limit %d\n", cl_fps_cur_limit);
  461. for (; i < MAX_NUM_INSTANCE_MTK_COOLER_FPS; i++) {
  462. unsigned int active;
  463. unsigned long curr_state;
  464. active = cl_fps_param[i];
  465. MTK_CL_FPS_GET_CURR_STATE(curr_state, cl_fps_state[i]);
  466. seq_printf(m, "mtk-cl-fps%02d %u 0x%x, state %lu\n", i, active, cl_fps_param[i], curr_state);
  467. }
  468. }
  469. return 0;
  470. }
  471. static ssize_t mtk_cl_fps_proc_write(struct file *filp, const char __user *buffer, size_t count, loff_t *data)
  472. {
  473. int len = 0;
  474. char desc[128];
  475. int klog_on, fps0, fps1, fps2, fps3;
  476. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  477. if (copy_from_user(desc, buffer, len))
  478. return 0;
  479. desc[len] = '\0';
  480. /**
  481. * sscanf format <klog_on> <mtk-cl-fps00> <mtk-cl-fps01> <mtk-cl-fps02> ...
  482. * <klog_on> can only be 0 or 1
  483. */
  484. if (NULL == data) {
  485. mtk_cooler_fps_dprintk("[%s] null data\n", __func__);
  486. return -EINVAL;
  487. }
  488. /* WARNING: Modify here if MAX_NUM_INSTANCE_MTK_COOLER_FPS is changed to other than 4 */
  489. #if (4 == MAX_NUM_INSTANCE_MTK_COOLER_FPS)
  490. if (1 <= sscanf(desc, "%d %d %d %d %d",
  491. &klog_on, &fps0, &fps1, &fps2, &fps3)) {
  492. if (klog_on == 0 || klog_on == 1)
  493. cl_fps_klog_on = klog_on;
  494. if (fps0 == 0)
  495. cl_fps_param[0] = 0;
  496. else if (fps0 >= 10 && fps0 <= 60)
  497. cl_fps_param[0] = fps0;
  498. if (fps1 == 0)
  499. cl_fps_param[1] = 0;
  500. else if (fps1 >= 10 && fps1 <= 60)
  501. cl_fps_param[1] = fps1;
  502. if (fps2 == 0)
  503. cl_fps_param[2] = 0;
  504. else if (fps2 >= 10 && fps2 <= 60)
  505. cl_fps_param[2] = fps2;
  506. if (fps3 == 0)
  507. cl_fps_param[3] = 0;
  508. else if (fps3 >= 10 && fps3 <= 60)
  509. cl_fps_param[3] = fps3;
  510. return count;
  511. }
  512. #else
  513. #error "Change correspondent part when changing MAX_NUM_INSTANCE_MTK_COOLER_FPS!"
  514. #endif
  515. mtk_cooler_fps_dprintk("[%s] bad arg\n", __func__);
  516. return -EINVAL;
  517. }
  518. static int mtk_cl_fps_proc_open(struct inode *inode, struct file *file)
  519. {
  520. return single_open(file, mtk_cl_fps_proc_read, NULL);
  521. }
  522. static const struct file_operations cl_fps_fops = {
  523. .owner = THIS_MODULE,
  524. .open = mtk_cl_fps_proc_open,
  525. .read = seq_read,
  526. .llseek = seq_lseek,
  527. .write = mtk_cl_fps_proc_write,
  528. .release = single_release,
  529. };
  530. static ssize_t fps_tm_count_write(struct file *filp, const char __user *buf, size_t len, loff_t *data)
  531. {
  532. char tmp[32] = {0};
  533. len = (len < (sizeof(tmp) - 1)) ? len : (sizeof(tmp) - 1);
  534. /* write data to the buffer */
  535. if (copy_from_user(tmp, buf, len))
  536. return -EFAULT;
  537. if (kstrtoint(tmp, 10, &tm_input_fps) == 0) {
  538. mtk_cooler_fps_dprintk("[%s] = %d\n", __func__, tm_input_fps);
  539. return len;
  540. }
  541. mtk_cooler_fps_dprintk("[%s] invalid input\n", __func__);
  542. return -EINVAL;
  543. }
  544. static int fps_tm_count_read(struct seq_file *m, void *v)
  545. {
  546. seq_printf(m, "%d\n", tm_input_fps);
  547. mtk_cooler_fps_dprintk("[%s] %d\n", __func__, tm_input_fps);
  548. return 0;
  549. }
  550. static int fps_tm_count_open(struct inode *inode, struct file *file)
  551. {
  552. return single_open(file, fps_tm_count_read, PDE_DATA(inode));
  553. }
  554. static const struct file_operations tm_fps_fops = {
  555. .owner = THIS_MODULE,
  556. .open = fps_tm_count_open,
  557. .read = seq_read,
  558. .llseek = seq_lseek,
  559. .write = fps_tm_count_write,
  560. .release = single_release,
  561. };
  562. /* ===== debug only===
  563. #define debugfs_entry(name) \
  564. do { \
  565. dentry_f = debugfs_create_u32(#name, S_IWUSR | S_IRUGO, _d, &name); \
  566. if (IS_ERR_OR_NULL(dentry_f)) { \
  567. pr_warn("Unable to create debugfsfile: " #name "\n"); \
  568. return; \
  569. } \
  570. } while (0)
  571. static void create_debugfs_entries(void)
  572. {
  573. struct dentry *dentry_f;
  574. struct dentry *_d;
  575. _d = debugfs_create_dir("clfps", NULL);
  576. if (IS_ERR_OR_NULL(_d)) {
  577. pr_info("unable to create debugfs directory\n");
  578. return;
  579. }
  580. debugfs_entry(fps_error_threshold);
  581. debugfs_entry(fps_stable_period);
  582. debugfs_entry(curr_fps_level);
  583. debugfs_entry(in_game_whitelist);
  584. debugfs_entry(fps_limit_always_on);
  585. debugfs_entry(leave_fps_limit_duration);
  586. debugfs_entry(in_game_low_fps);
  587. debugfs_entry(gpu_loading_threshold);
  588. }
  589. #undef debugfs_entry
  590. ===== debug only === */
  591. static int __init mtk_cooler_fps_init(void)
  592. {
  593. int ret = 0;
  594. int err = 0;
  595. int i;
  596. for (i = MAX_NUM_INSTANCE_MTK_COOLER_FPS; i-- > 0; ) {
  597. cl_fps_dev[i] = NULL;
  598. cl_fps_state[i] = 0;
  599. }
  600. mtk_cooler_fps_dprintk("init\n");
  601. err = mtk_cooler_fps_register_ltf();
  602. if (err)
  603. goto err_unreg;
  604. /* switch device to sent the (fps limit)uevent */
  605. fps_switch_data.name = "fps";
  606. fps_switch_data.index = 0;
  607. fps_switch_data.state = 60; /* original 60 frames */
  608. ret = switch_dev_register(&fps_switch_data);
  609. if (ret)
  610. mtk_cooler_fps_dprintk_always("[%s] switch_dev_register failed, returned:%d!\n",
  611. __func__, ret);
  612. /* create a proc file */
  613. {
  614. struct proc_dir_entry *entry = NULL;
  615. struct proc_dir_entry *dir_entry = NULL;
  616. struct proc_dir_entry *fps_tm_proc_dir = NULL;
  617. fps_tm_proc_dir = proc_mkdir("fps_tm", NULL);
  618. if (!fps_tm_proc_dir)
  619. mtk_cooler_fps_dprintk_always("[%s]: mkdir /proc/fps_tm failed\n", __func__);
  620. else
  621. entry = proc_create("fps_count", S_IRWXUGO, fps_tm_proc_dir, &tm_fps_fops);
  622. dir_entry = mtk_thermal_get_proc_drv_therm_dir_entry();
  623. if (!dir_entry)
  624. mtk_cooler_fps_dprintk_always("[%s]: mkdir /proc/driver/thermal failed\n", __func__);
  625. else {
  626. entry = proc_create("clfps", S_IRUGO | S_IWUSR | S_IWGRP, dir_entry, &cl_fps_fops);
  627. if (entry)
  628. proc_set_user(entry, uid, gid);
  629. }
  630. #if ADAPTIVE_FPS_COOLER
  631. reset_fps_level();
  632. if (dir_entry) {
  633. entry = proc_create("clfps_adp", S_IRUGO | S_IWUSR | S_IWGRP,
  634. dir_entry, &clfps_adp_fops);
  635. if (entry)
  636. proc_set_user(entry, uid, gid);
  637. entry = proc_create("clfps_level", S_IRUGO | S_IWUSR | S_IWGRP,
  638. dir_entry, &clfps_level_fops);
  639. if (entry)
  640. proc_set_user(entry, uid, gid);
  641. }
  642. /* ===== debug only===
  643. create_debugfs_entries();
  644. ===== debug only === */
  645. #endif
  646. return 0;
  647. }
  648. err_unreg:
  649. mtk_cooler_fps_unregister_ltf();
  650. return err;
  651. }
  652. static void __exit mtk_cooler_fps_exit(void)
  653. {
  654. mtk_cooler_fps_dprintk("exit\n");
  655. /* remove the proc file */
  656. remove_proc_entry("clfps", NULL);
  657. mtk_cooler_fps_unregister_ltf();
  658. }
  659. module_init(mtk_cooler_fps_init);
  660. module_exit(mtk_cooler_fps_exit);