mtk_cooler_bcct.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 <linux/seq_file.h>
  8. #include <asm/uaccess.h>
  9. #include <linux/err.h>
  10. #include <linux/syscalls.h>
  11. #include "mt-plat/mtk_thermal_monitor.h"
  12. #include <charging.h>
  13. #include <tmp_battery.h>
  14. #include <linux/uidgid.h>
  15. /* ************************************ */
  16. /* Weak functions */
  17. /* ************************************ */
  18. int __attribute__ ((weak))
  19. get_bat_charging_current_level(void)
  20. {
  21. pr_err("E_WF: %s doesn't exist\n", __func__);
  22. return 500;
  23. }
  24. CHARGER_TYPE __attribute__ ((weak))
  25. mt_get_charger_type(void)
  26. {
  27. pr_err("E_WF: %s doesn't exist\n", __func__);
  28. return STANDARD_HOST;
  29. }
  30. int __attribute__ ((weak))
  31. set_bat_charging_current_limit(int current_limit)
  32. {
  33. pr_err("E_WF: %s doesn't exist\n", __func__);
  34. return 0;
  35. }
  36. /* ************************************ */
  37. #define mtk_cooler_bcct_dprintk_always(fmt, args...) \
  38. pr_debug("[thermal/cooler/bcct]" fmt, ##args)
  39. #define mtk_cooler_bcct_dprintk(fmt, args...) \
  40. do { \
  41. if (1 == cl_bcct_klog_on) \
  42. pr_debug("[thermal/cooler/bcct]" fmt, ##args); \
  43. } while (0)
  44. #define MAX_NUM_INSTANCE_MTK_COOLER_BCCT 3
  45. #define MTK_CL_BCCT_GET_LIMIT(limit, state) \
  46. {(limit) = (short) (((unsigned long) (state))>>16); }
  47. #define MTK_CL_BCCT_SET_LIMIT(limit, state) \
  48. {(state) = ((((unsigned long) (state))&0xFFFF) | ((short) limit<<16)); }
  49. #define MTK_CL_BCCT_GET_CURR_STATE(curr_state, state) \
  50. {(curr_state) = (((unsigned long) (state))&0xFFFF); }
  51. #define MTK_CL_BCCT_SET_CURR_STATE(curr_state, state) \
  52. do { \
  53. if (0 == (curr_state)) \
  54. state &= ~0x1; \
  55. else \
  56. state |= 0x1; \
  57. } while (0)
  58. static kuid_t uid = KUIDT_INIT(0);
  59. static kgid_t gid = KGIDT_INIT(1000);
  60. static int cl_bcct_klog_on;
  61. static struct thermal_cooling_device *cl_bcct_dev[MAX_NUM_INSTANCE_MTK_COOLER_BCCT] = { 0 };
  62. static unsigned long cl_bcct_state[MAX_NUM_INSTANCE_MTK_COOLER_BCCT] = { 0 };
  63. static int cl_bcct_cur_limit = 65535;
  64. static void mtk_cl_bcct_set_bcct_limit(void)
  65. {
  66. /* TODO: optimize */
  67. int i = 0;
  68. int min_limit = 65535;
  69. for (; i < MAX_NUM_INSTANCE_MTK_COOLER_BCCT; i++) {
  70. unsigned long curr_state;
  71. MTK_CL_BCCT_GET_CURR_STATE(curr_state, cl_bcct_state[i]);
  72. if (1 == curr_state) {
  73. int limit;
  74. MTK_CL_BCCT_GET_LIMIT(limit, cl_bcct_state[i]);
  75. if ((min_limit > limit) && (limit > 0))
  76. min_limit = limit;
  77. }
  78. }
  79. if (min_limit != cl_bcct_cur_limit) {
  80. cl_bcct_cur_limit = min_limit;
  81. if (65535 <= cl_bcct_cur_limit) {
  82. set_bat_charging_current_limit(-1);
  83. mtk_cooler_bcct_dprintk_always("mtk_cl_bcct_set_bcct_limit() limit=-1\n");
  84. } else {
  85. set_bat_charging_current_limit(cl_bcct_cur_limit);
  86. mtk_cooler_bcct_dprintk_always("mtk_cl_bcct_set_bcct_limit() limit=%d\n",
  87. cl_bcct_cur_limit);
  88. }
  89. mtk_cooler_bcct_dprintk_always("mtk_cl_bcct_set_bcct_limit() real limit=%d\n",
  90. get_bat_charging_current_level() / 100);
  91. }
  92. }
  93. static int mtk_cl_bcct_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
  94. {
  95. *state = 1;
  96. mtk_cooler_bcct_dprintk("mtk_cl_bcct_get_max_state() %s %lu\n", cdev->type, *state);
  97. return 0;
  98. }
  99. static int mtk_cl_bcct_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
  100. {
  101. MTK_CL_BCCT_GET_CURR_STATE(*state, *((unsigned long *)cdev->devdata));
  102. mtk_cooler_bcct_dprintk("mtk_cl_bcct_get_cur_state() %s %lu\n", cdev->type, *state);
  103. mtk_cooler_bcct_dprintk("mtk_cl_bcct_get_cur_state() %s limit=%d\n", cdev->type,
  104. get_bat_charging_current_level() / 100);
  105. return 0;
  106. }
  107. static int mtk_cl_bcct_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  108. {
  109. mtk_cooler_bcct_dprintk("mtk_cl_bcct_set_cur_state() %s %lu\n", cdev->type, state);
  110. MTK_CL_BCCT_SET_CURR_STATE(state, *((unsigned long *)cdev->devdata));
  111. mtk_cl_bcct_set_bcct_limit();
  112. mtk_cooler_bcct_dprintk("mtk_cl_bcct_set_cur_state() %s limit=%d\n", cdev->type,
  113. get_bat_charging_current_level() / 100);
  114. return 0;
  115. }
  116. /* bind fan callbacks to fan device */
  117. static struct thermal_cooling_device_ops mtk_cl_bcct_ops = {
  118. .get_max_state = mtk_cl_bcct_get_max_state,
  119. .get_cur_state = mtk_cl_bcct_get_cur_state,
  120. .set_cur_state = mtk_cl_bcct_set_cur_state,
  121. };
  122. static int mtk_cooler_bcct_register_ltf(void)
  123. {
  124. int i;
  125. mtk_cooler_bcct_dprintk("register ltf\n");
  126. for (i = MAX_NUM_INSTANCE_MTK_COOLER_BCCT; i-- > 0;) {
  127. char temp[20] = { 0 };
  128. sprintf(temp, "mtk-cl-bcct%02d", i);
  129. /* put bcct state to cooler devdata */
  130. cl_bcct_dev[i] = mtk_thermal_cooling_device_register(temp, (void *)&cl_bcct_state[i],
  131. &mtk_cl_bcct_ops);
  132. }
  133. return 0;
  134. }
  135. static void mtk_cooler_bcct_unregister_ltf(void)
  136. {
  137. int i;
  138. mtk_cooler_bcct_dprintk("unregister ltf\n");
  139. for (i = MAX_NUM_INSTANCE_MTK_COOLER_BCCT; i-- > 0;) {
  140. if (cl_bcct_dev[i]) {
  141. mtk_thermal_cooling_device_unregister(cl_bcct_dev[i]);
  142. cl_bcct_dev[i] = NULL;
  143. cl_bcct_state[i] = 0;
  144. }
  145. }
  146. }
  147. #if 0
  148. static int _mtk_cl_bcct_proc_read(char *buf, char **start, off_t off, int count, int *eof,
  149. void *data)
  150. {
  151. int len = 0;
  152. char *p = buf;
  153. mtk_cooler_bcct_dprintk("[_mtk_cl_bcct_proc_read] invoked.\n");
  154. /**
  155. * The format to print out:
  156. * kernel_log <0 or 1>
  157. * <mtk-cl-bcct<ID>> <bcc limit>
  158. * ..
  159. */
  160. if (NULL == data) {
  161. mtk_cooler_bcct_dprintk("[_mtk_cl_bcct_proc_read] null data\n");
  162. } else {
  163. int i = 0;
  164. p += sprintf(p, "klog %d\n", cl_bcct_klog_on);
  165. p += sprintf(p, "curr_limit %d\n", cl_bcct_cur_limit);
  166. for (; i < MAX_NUM_INSTANCE_MTK_COOLER_BCCT; i++) {
  167. int limit;
  168. unsigned int curr_state;
  169. MTK_CL_BCCT_GET_LIMIT(limit, cl_bcct_state[i]);
  170. MTK_CL_BCCT_GET_CURR_STATE(curr_state, cl_bcct_state[i]);
  171. p += sprintf(p, "mtk-cl-bcct%02d %d mA, state %d\n", i, limit, curr_state);
  172. }
  173. }
  174. *start = buf + off;
  175. len = p - buf;
  176. if (len > off)
  177. len -= off;
  178. else
  179. len = 0;
  180. return len < count ? len : count;
  181. }
  182. static ssize_t _mtk_cl_bcct_proc_write(struct file *file, const char *buffer, unsigned long count,
  183. void *data)
  184. {
  185. int len = 0;
  186. char desc[128];
  187. int klog_on, limit0, limit1, limit2;
  188. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  189. if (copy_from_user(desc, buffer, len))
  190. return 0;
  191. desc[len] = '\0';
  192. /**
  193. * sscanf format <klog_on> <mtk-cl-bcct00 limit> <mtk-cl-bcct01 limit> ...
  194. * <klog_on> can only be 0 or 1
  195. * <mtk-cl-bcct00 limit> can only be positive integer or -1 to denote no limit
  196. */
  197. if (NULL == data) {
  198. mtk_cooler_bcct_dprintk("[_mtk_cl_bcct_proc_write] null data\n");
  199. return -EINVAL;
  200. }
  201. /* WARNING: Modify here if MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS is changed to other than 3 */
  202. #if (3 == MAX_NUM_INSTANCE_MTK_COOLER_BCCT)
  203. MTK_CL_BCCT_SET_LIMIT(-1, cl_bcct_state[0]);
  204. MTK_CL_BCCT_SET_LIMIT(-1, cl_bcct_state[1]);
  205. MTK_CL_BCCT_SET_LIMIT(-1, cl_bcct_state[2]);
  206. if (1 <= sscanf(desc, "%d %d %d %d", &klog_on, &limit0, &limit1, &limit2)) {
  207. if (klog_on == 0 || klog_on == 1)
  208. cl_bcct_klog_on = klog_on;
  209. if (limit0 >= -1)
  210. MTK_CL_BCCT_SET_LIMIT(limit0, cl_bcct_state[0]);
  211. if (limit1 >= -1)
  212. MTK_CL_BCCT_SET_LIMIT(limit1, cl_bcct_state[1]);
  213. if (limit2 >= -1)
  214. MTK_CL_BCCT_SET_LIMIT(limit2, cl_bcct_state[2]);
  215. return count;
  216. }
  217. #else
  218. #error "Change correspondent part when changing MAX_NUM_INSTANCE_MTK_COOLER_BCCT!"
  219. #endif
  220. mtk_cooler_bcct_dprintk("[_mtk_cl_bcct_proc_write] bad argument\n");
  221. return -EINVAL;
  222. }
  223. #endif
  224. static ssize_t _cl_bcct_write(struct file *filp, const char __user *buf, size_t count, loff_t *data)
  225. {
  226. /* int ret = 0; */
  227. char tmp[128] = { 0 };
  228. int klog_on, limit0, limit1, limit2;
  229. int len = 0;
  230. len = (count < (128 - 1)) ? count : (128 - 1);
  231. /* write data to the buffer */
  232. if (copy_from_user(tmp, buf, len))
  233. return -EFAULT;
  234. /**
  235. * sscanf format <klog_on> <mtk-cl-bcct00 limit> <mtk-cl-bcct01 limit> ...
  236. * <klog_on> can only be 0 or 1
  237. * <mtk-cl-bcct00 limit> can only be positive integer or -1 to denote no limit
  238. */
  239. if (NULL == data) {
  240. mtk_cooler_bcct_dprintk("[_mtk_cl_bcct_proc_write] null data\n");
  241. return -EINVAL;
  242. }
  243. /* WARNING: Modify here if MTK_THERMAL_MONITOR_COOLER_MAX_EXTRA_CONDITIONS is changed to other than 3 */
  244. #if (3 == MAX_NUM_INSTANCE_MTK_COOLER_BCCT)
  245. MTK_CL_BCCT_SET_LIMIT(-1, cl_bcct_state[0]);
  246. MTK_CL_BCCT_SET_LIMIT(-1, cl_bcct_state[1]);
  247. MTK_CL_BCCT_SET_LIMIT(-1, cl_bcct_state[2]);
  248. if (1 <= sscanf(tmp, "%d %d %d %d", &klog_on, &limit0, &limit1, &limit2)) {
  249. if (klog_on == 0 || klog_on == 1)
  250. cl_bcct_klog_on = klog_on;
  251. if (limit0 >= -1)
  252. MTK_CL_BCCT_SET_LIMIT(limit0, cl_bcct_state[0]);
  253. if (limit1 >= -1)
  254. MTK_CL_BCCT_SET_LIMIT(limit1, cl_bcct_state[1]);
  255. if (limit2 >= -1)
  256. MTK_CL_BCCT_SET_LIMIT(limit2, cl_bcct_state[2]);
  257. return len;
  258. }
  259. #else
  260. #error "Change correspondent part when changing MAX_NUM_INSTANCE_MTK_COOLER_BCCT!"
  261. #endif
  262. mtk_cooler_bcct_dprintk("[_mtk_cl_bcct_proc_write] bad argument\n");
  263. return -EINVAL;
  264. }
  265. static int _cl_bcct_read(struct seq_file *m, void *v)
  266. {
  267. /**
  268. * The format to print out:
  269. * kernel_log <0 or 1>
  270. * <mtk-cl-bcct<ID>> <bcc limit>
  271. * ..
  272. */
  273. mtk_cooler_bcct_dprintk("_cl_bcct_read invoked.\n");
  274. {
  275. int i = 0;
  276. seq_printf(m, "klog %d\n", cl_bcct_klog_on);
  277. seq_printf(m, "curr_limit %d\n", cl_bcct_cur_limit);
  278. for (; i < MAX_NUM_INSTANCE_MTK_COOLER_BCCT; i++) {
  279. int limit;
  280. unsigned int curr_state;
  281. MTK_CL_BCCT_GET_LIMIT(limit, cl_bcct_state[i]);
  282. MTK_CL_BCCT_GET_CURR_STATE(curr_state, cl_bcct_state[i]);
  283. seq_printf(m, "mtk-cl-bcct%02d %d mA, state %d\n", i, limit, curr_state);
  284. }
  285. }
  286. return 0;
  287. }
  288. static int _cl_bcct_open(struct inode *inode, struct file *file)
  289. {
  290. return single_open(file, _cl_bcct_read, PDE_DATA(inode));
  291. }
  292. static const struct file_operations _cl_bcct_fops = {
  293. .owner = THIS_MODULE,
  294. .open = _cl_bcct_open,
  295. .read = seq_read,
  296. .llseek = seq_lseek,
  297. .write = _cl_bcct_write,
  298. .release = single_release,
  299. };
  300. static int __init mtk_cooler_bcct_init(void)
  301. {
  302. int err = 0;
  303. int i;
  304. for (i = MAX_NUM_INSTANCE_MTK_COOLER_BCCT; i-- > 0;) {
  305. cl_bcct_dev[i] = NULL;
  306. cl_bcct_state[i] = 0;
  307. }
  308. /* cl_bcct_dev = NULL; */
  309. mtk_cooler_bcct_dprintk("init\n");
  310. err = mtk_cooler_bcct_register_ltf();
  311. if (err)
  312. goto err_unreg;
  313. /* create a proc file */
  314. {
  315. struct proc_dir_entry *entry = NULL;
  316. struct proc_dir_entry *dir_entry = NULL;
  317. dir_entry = mtk_thermal_get_proc_drv_therm_dir_entry();
  318. if (!dir_entry) {
  319. mtk_cooler_bcct_dprintk("[%s]: mkdir /proc/driver/thermal failed\n",
  320. __func__);
  321. }
  322. entry =
  323. proc_create("clbcct", S_IRUGO | S_IWUSR | S_IWGRP, dir_entry, &_cl_bcct_fops);
  324. if (!entry)
  325. mtk_cooler_bcct_dprintk_always("%s clbcct creation failed\n", __func__);
  326. else
  327. proc_set_user(entry, uid, gid);
  328. }
  329. return 0;
  330. err_unreg:
  331. mtk_cooler_bcct_unregister_ltf();
  332. return err;
  333. }
  334. static void __exit mtk_cooler_bcct_exit(void)
  335. {
  336. mtk_cooler_bcct_dprintk("exit\n");
  337. /* remove the proc file */
  338. remove_proc_entry("driver/mtk-cl-bcct", NULL);
  339. mtk_cooler_bcct_unregister_ltf();
  340. }
  341. module_init(mtk_cooler_bcct_init);
  342. module_exit(mtk_cooler_bcct_exit);