mtk_cooler_mddulthro.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/types.h>
  4. #include <linux/kobject.h>
  5. #include <linux/proc_fs.h>
  6. #include <asm/uaccess.h>
  7. #include <linux/err.h>
  8. #include <linux/syscalls.h>
  9. #include "mt-plat/mtk_thermal_monitor.h"
  10. #include <mt_cl_amddulthro.h>
  11. #define mtk_cooler_mddulthro_dprintk_always(fmt, args...) \
  12. pr_debug("thermal/cooler/mddulthro" fmt, ##args)
  13. #define mtk_cooler_mddulthro_dprintk(fmt, args...) \
  14. do { \
  15. if (1 == cl_mddulthro_klog_on) \
  16. pr_debug("thermal/cooler/mddulthro" fmt, ##args); \
  17. } while (0)
  18. #define MAX_NUM_INSTANCE_MTK_COOLER_MDDULTHRO 3
  19. #define MTK_CL_MDDULTHRO_GET_LIMIT(limit, state) \
  20. { (limit) = (short) (((unsigned long) (state))>>16); }
  21. #define MTK_CL_MDDULTHRO_SET_LIMIT(limit, state) \
  22. { state = ((((unsigned long) (state))&0xFFFF) | ((short) limit<<16)); }
  23. #define MTK_CL_MDDULTHRO_GET_CURR_STATE(curr_state, state) \
  24. { curr_state = (((unsigned long) (state))&0xFFFF); }
  25. #define MTK_CL_MDDULTHRO_SET_CURR_STATE(curr_state, state) \
  26. do { \
  27. if (0 == curr_state) \
  28. state &= ~0x1; \
  29. else \
  30. state |= 0x1; \
  31. } while (0)
  32. static int cl_mddulthro_klog_on;
  33. static struct thermal_cooling_device *cl_mddulthro_dev[MAX_NUM_INSTANCE_MTK_COOLER_MDDULTHRO] = { 0 };
  34. static unsigned long cl_mddulthro_state[MAX_NUM_INSTANCE_MTK_COOLER_MDDULTHRO] = { 0 };
  35. static int cl_mddulthro_cur_limit;
  36. static void mtk_cl_mddulthro_set_mddulthro_limit(void)
  37. {
  38. /* TODO: optimize */
  39. int i = 0;
  40. int min_limit = 0;
  41. for (; i < MAX_NUM_INSTANCE_MTK_COOLER_MDDULTHRO; i++) {
  42. unsigned long curr_state;
  43. MTK_CL_MDDULTHRO_GET_CURR_STATE(curr_state, cl_mddulthro_state[i]);
  44. if (1 == curr_state) {
  45. int limit;
  46. MTK_CL_MDDULTHRO_GET_LIMIT(limit, cl_mddulthro_state[i]);
  47. if ((min_limit < limit) && (limit > 0))
  48. min_limit = limit;
  49. }
  50. }
  51. if (min_limit != cl_mddulthro_cur_limit) {
  52. cl_mddulthro_cur_limit = min_limit;
  53. #if 1
  54. if (0 >= cl_mddulthro_cur_limit) {
  55. int ret = amddulthro_backoff(0);
  56. mtk_cooler_mddulthro_dprintk_always
  57. ("mtk_cl_mddulthro_set_mddulthro_limit() ret %d limit=0\n", ret);
  58. } else {
  59. int ret = amddulthro_backoff(cl_mddulthro_cur_limit);
  60. mtk_cooler_mddulthro_dprintk_always
  61. ("mtk_cl_mddulthro_set_mddulthro_limit() ret %d limit=%d\n",
  62. cl_mddulthro_cur_limit);
  63. }
  64. #endif
  65. }
  66. }
  67. static int mtk_cl_mddulthro_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
  68. {
  69. *state = 1;
  70. mtk_cooler_mddulthro_dprintk("mtk_cl_mddulthro_get_max_state() %s %d\n", cdev->type,
  71. *state);
  72. return 0;
  73. }
  74. static int mtk_cl_mddulthro_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
  75. {
  76. MTK_CL_MDDULTHRO_GET_CURR_STATE(*state, *((unsigned long *)cdev->devdata));
  77. mtk_cooler_mddulthro_dprintk("mtk_cl_mddulthro_get_cur_state() %s %d\n", cdev->type,
  78. *state);
  79. return 0;
  80. }
  81. static int mtk_cl_mddulthro_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  82. {
  83. mtk_cooler_mddulthro_dprintk("mtk_cl_mddulthro_set_cur_state() %s %d\n", cdev->type, state);
  84. MTK_CL_MDDULTHRO_SET_CURR_STATE(state, *((unsigned long *)cdev->devdata));
  85. mtk_cl_mddulthro_set_mddulthro_limit();
  86. return 0;
  87. }
  88. /* bind fan callbacks to fan device */
  89. static struct thermal_cooling_device_ops mtk_cl_mddulthro_ops = {
  90. .get_max_state = mtk_cl_mddulthro_get_max_state,
  91. .get_cur_state = mtk_cl_mddulthro_get_cur_state,
  92. .set_cur_state = mtk_cl_mddulthro_set_cur_state,
  93. };
  94. static int mtk_cooler_mddulthro_register_ltf(void)
  95. {
  96. int i;
  97. mtk_cooler_mddulthro_dprintk("register ltf\n");
  98. for (i = MAX_NUM_INSTANCE_MTK_COOLER_MDDULTHRO; i-- > 0;) {
  99. char temp[20] = { 0 };
  100. sprintf(temp, "mtk-cl-mddulthro%02d", i);
  101. /* put mddulthro state to cooler devdata */
  102. cl_mddulthro_dev[i] = mtk_thermal_cooling_device_register(temp, (void *)&cl_mddulthro_state[i],
  103. &mtk_cl_mddulthro_ops);
  104. }
  105. return 0;
  106. }
  107. static void mtk_cooler_mddulthro_unregister_ltf(void)
  108. {
  109. int i;
  110. mtk_cooler_mddulthro_dprintk("unregister ltf\n");
  111. for (i = MAX_NUM_INSTANCE_MTK_COOLER_MDDULTHRO; i-- > 0;) {
  112. if (cl_mddulthro_dev[i]) {
  113. mtk_thermal_cooling_device_unregister(cl_mddulthro_dev[i]);
  114. cl_mddulthro_dev[i] = NULL;
  115. cl_mddulthro_state[i] = 0;
  116. }
  117. }
  118. }
  119. static int _mtk_cl_mddulthro_proc_read(char *buf, char **start, off_t off, int count, int *eof,
  120. void *data)
  121. {
  122. int len = 0;
  123. char *p = buf;
  124. mtk_cooler_mddulthro_dprintk("[_mtk_cl_mddulthro_proc_read] invoked.\n");
  125. /**
  126. * The format to print out:
  127. * kernel_log <0 or 1>
  128. * <mtk-cl-mddulthro<ID>> <bcc limit>
  129. * ..
  130. */
  131. if (NULL == data) {
  132. mtk_cooler_mddulthro_dprintk("[_mtk_cl_mddulthro_proc_read] null data\n");
  133. } else {
  134. int i = 0;
  135. p += sprintf(p, "klog %d\n", cl_mddulthro_klog_on);
  136. p += sprintf(p, "curr_limit %d\n", cl_mddulthro_cur_limit);
  137. for (; i < MAX_NUM_INSTANCE_MTK_COOLER_MDDULTHRO; i++) {
  138. int limit;
  139. unsigned int curr_state;
  140. MTK_CL_MDDULTHRO_GET_LIMIT(limit, cl_mddulthro_state[i]);
  141. MTK_CL_MDDULTHRO_GET_CURR_STATE(curr_state, cl_mddulthro_state[i]);
  142. p += sprintf(p, "mtk-cl-mddulthro%02d lv %d, state %d\n", i, limit,
  143. curr_state);
  144. }
  145. }
  146. *start = buf + off;
  147. len = p - buf;
  148. if (len > off)
  149. len -= off;
  150. else
  151. len = 0;
  152. return len < count ? len : count;
  153. }
  154. static ssize_t _mtk_cl_mddulthro_proc_write(struct file *file, const char *buffer,
  155. unsigned long count, void *data)
  156. {
  157. int len = 0;
  158. char desc[128];
  159. int klog_on, limit0, limit1, limit2;
  160. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  161. if (copy_from_user(desc, buffer, len))
  162. return 0;
  163. desc[len] = '\0';
  164. /**
  165. * sscanf format <klog_on> <mtk-cl-mddulthro00 limit> <mtk-cl-mddulthro01 limit> ...
  166. * <klog_on> can only be 0 or 1
  167. * <mtk-cl-mddulthro00 limit> can only be positive integer or -1 to denote no limit
  168. */
  169. if (NULL == data) {
  170. mtk_cooler_mddulthro_dprintk("[_mtk_cl_mddulthro_proc_write] null data\n");
  171. return -EINVAL;
  172. }
  173. /* WARNING: Modify here if MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS is changed to other than 3 */
  174. #if (3 == MAX_NUM_INSTANCE_MTK_COOLER_MDDULTHRO)
  175. MTK_CL_MDDULTHRO_SET_LIMIT(-1, cl_mddulthro_state[0]);
  176. MTK_CL_MDDULTHRO_SET_LIMIT(-1, cl_mddulthro_state[1]);
  177. MTK_CL_MDDULTHRO_SET_LIMIT(-1, cl_mddulthro_state[2]);
  178. if (1 <= sscanf(desc, "%d %d %d %d", &klog_on, &limit0, &limit1, &limit2)) {
  179. if (klog_on == 0 || klog_on == 1)
  180. cl_mddulthro_klog_on = klog_on;
  181. if (limit0 >= -1)
  182. MTK_CL_MDDULTHRO_SET_LIMIT(limit0, cl_mddulthro_state[0]);
  183. if (limit1 >= -1)
  184. MTK_CL_MDDULTHRO_SET_LIMIT(limit1, cl_mddulthro_state[1]);
  185. if (limit2 >= -1)
  186. MTK_CL_MDDULTHRO_SET_LIMIT(limit2, cl_mddulthro_state[2]);
  187. return count;
  188. }
  189. #else
  190. #error "Change correspondent part when changing MAX_NUM_INSTANCE_MTK_COOLER_MDDULTHRO!"
  191. #endif
  192. mtk_cooler_mddulthro_dprintk("[_mtk_cl_mddulthro_proc_write] bad argument\n");
  193. return -EINVAL;
  194. }
  195. static int __init mtk_cooler_mddulthro_init(void)
  196. {
  197. int err = 0;
  198. int i;
  199. for (i = MAX_NUM_INSTANCE_MTK_COOLER_MDDULTHRO; i-- > 0;) {
  200. cl_mddulthro_dev[i] = NULL;
  201. cl_mddulthro_state[i] = 0;
  202. }
  203. /* cl_mddulthro_dev = NULL; */
  204. mtk_cooler_mddulthro_dprintk("init\n");
  205. err = mtk_cooler_mddulthro_register_ltf();
  206. if (err)
  207. goto err_unreg;
  208. /* create a proc file */
  209. {
  210. struct proc_dir_entry *entry = NULL;
  211. entry =
  212. create_proc_entry("driver/mtk-cl-mddulthro", S_IRUGO | S_IWUSR | S_IWGRP, NULL);
  213. if (NULL != entry) {
  214. entry->read_proc = _mtk_cl_mddulthro_proc_read;
  215. entry->write_proc = _mtk_cl_mddulthro_proc_write;
  216. entry->data = cl_mddulthro_state;
  217. entry->gid = 1000; /* allow system process to write this proc */
  218. }
  219. mtk_cooler_mddulthro_dprintk("[mtk_cooler_mddulthro_init] proc file created: %x\n",
  220. entry->data);
  221. }
  222. return 0;
  223. err_unreg:
  224. mtk_cooler_mddulthro_unregister_ltf();
  225. return err;
  226. }
  227. static void __exit mtk_cooler_mddulthro_exit(void)
  228. {
  229. mtk_cooler_mddulthro_dprintk("exit\n");
  230. /* remove the proc file */
  231. remove_proc_entry("driver/mtk-cl-mddulthro", NULL);
  232. mtk_cooler_mddulthro_unregister_ltf();
  233. }
  234. module_init(mtk_cooler_mddulthro_init);
  235. module_exit(mtk_cooler_mddulthro_exit);