mtk_ts_battery.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. #include <linux/version.h>
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #include <linux/dmi.h>
  5. #include <linux/acpi.h>
  6. #include <linux/thermal.h>
  7. #include <linux/platform_device.h>
  8. #include <mt-plat/aee.h>
  9. #include <linux/types.h>
  10. #include <linux/delay.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/syscalls.h>
  13. #include <linux/sched.h>
  14. #include <linux/writeback.h>
  15. #include <asm/uaccess.h>
  16. #include "mt-plat/mtk_thermal_monitor.h"
  17. #include "mtk_thermal_typedefs.h"
  18. #include "mach/mt_thermal.h"
  19. #include <tmp_battery.h>
  20. #include <linux/uidgid.h>
  21. #include <linux/slab.h>
  22. /* ************************************ */
  23. /* Weak functions */
  24. /* ************************************ */
  25. int __attribute__ ((weak))
  26. read_tbat_value(void)
  27. {
  28. pr_err("E_WF: %s doesn't exist\n", __func__);
  29. return 30;
  30. }
  31. /* ************************************ */
  32. static kuid_t uid = KUIDT_INIT(0);
  33. static kgid_t gid = KGIDT_INIT(1000);
  34. static unsigned int interval; /* seconds, 0 : no auto polling */
  35. static int trip_temp[10] = { 120000, 110000, 100000, 90000, 80000, 70000, 65000, 60000, 55000, 50000 };
  36. /* static unsigned int cl_dev_dis_charge_state = 0; */
  37. static unsigned int cl_dev_sysrst_state;
  38. static struct thermal_zone_device *thz_dev;
  39. /* static struct thermal_cooling_device *cl_dev_dis_charge; */
  40. static struct thermal_cooling_device *cl_dev_sysrst;
  41. static int mtktsbattery_debug_log;
  42. static int kernelmode;
  43. static int g_THERMAL_TRIP[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  44. static int num_trip;
  45. static char g_bind0[20] = { 0 };
  46. static char g_bind1[20] = { 0 };
  47. static char g_bind2[20] = { 0 };
  48. static char g_bind3[20] = { 0 };
  49. static char g_bind4[20] = { 0 };
  50. static char g_bind5[20] = { 0 };
  51. static char g_bind6[20] = { 0 };
  52. static char g_bind7[20] = { 0 };
  53. static char g_bind8[20] = { 0 };
  54. static char g_bind9[20] = { 0 };
  55. /**
  56. * If curr_temp >= polling_trip_temp1, use interval
  57. * else if cur_temp >= polling_trip_temp2 && curr_temp < polling_trip_temp1, use interval*polling_factor1
  58. * else, use interval*polling_factor2
  59. */
  60. static int polling_trip_temp1 = 40000;
  61. static int polling_trip_temp2 = 20000;
  62. static int polling_factor1 = 5000;
  63. static int polling_factor2 = 10000;
  64. /* static int battery_write_flag=0; */
  65. #define mtktsbattery_TEMP_CRIT 60000 /* 60.000 degree Celsius */
  66. #define mtktsbattery_dprintk(fmt, args...) \
  67. do { \
  68. if (mtktsbattery_debug_log) { \
  69. pr_debug("Power/Battery_Thermal" fmt, ##args); \
  70. } \
  71. } while (0)
  72. /*
  73. * kernel fopen/fclose
  74. */
  75. /*
  76. static mm_segment_t oldfs;
  77. static void my_close(int fd)
  78. {
  79. set_fs(oldfs);
  80. sys_close(fd);
  81. }
  82. static int my_open(char *fname, int flag)
  83. {
  84. oldfs = get_fs();
  85. set_fs(KERNEL_DS);
  86. return sys_open(fname, flag, 0);
  87. }
  88. */
  89. static int get_hw_battery_temp(void)
  90. {
  91. /*
  92. int fd;
  93. char buf[64];
  94. char *pmtdbufp = NULL;
  95. ssize_t pmtdsize;
  96. char *pvalue = NULL;
  97. int got_value=0;
  98. //open file and read current value
  99. fd = my_open("/sys/class/power_supply/battery/batt_temp", O_RDONLY);
  100. if (fd < 0)
  101. {
  102. mtktsbattery_dprintk("[get_hw_battery_temp]: open file fail");
  103. return 0;
  104. }
  105. mtktsbattery_dprintk("[get_hw_battery_temp]: open file ok");
  106. buf[sizeof(buf) - 1] = '\0';
  107. pmtdsize = sys_read(fd, buf, sizeof(buf) - 1);
  108. pmtdbufp = buf;
  109. got_value = simple_strtol(pmtdbufp,&pvalue,10);
  110. // close file
  111. my_close(fd);
  112. // debug
  113. mtktsbattery_dprintk("[get_hw_battery_temp]: got_value=%d\n", got_value);
  114. return got_value;
  115. */
  116. int ret = 0;
  117. #if defined(CONFIG_POWER_EXT)
  118. /* EVB */
  119. ret = -1270;
  120. #else
  121. /* Phone */
  122. ret = read_tbat_value();
  123. ret = ret * 10;
  124. #endif
  125. return ret;
  126. }
  127. static DEFINE_MUTEX(Battery_lock);
  128. int ts_battery_at_boot_time = 0;
  129. static int mtktsbattery_get_hw_temp(void)
  130. {
  131. int t_ret = 0;
  132. static int battery[60] = { 0 };
  133. static int counter = 0, first_time;
  134. if (ts_battery_at_boot_time == 0) {
  135. ts_battery_at_boot_time = 1;
  136. mtktsbattery_dprintk
  137. ("[mtktsbattery_get_hw_temp] at boot time, return 25000 as default\n");
  138. battery[counter] = 25000;
  139. counter++;
  140. return 25000;
  141. }
  142. mutex_lock(&Battery_lock);
  143. /* get HW battery temp (TSBATTERY) */
  144. /* cat /sys/class/power_supply/battery/batt_temp */
  145. t_ret = get_hw_battery_temp();
  146. t_ret = t_ret * 100;
  147. mutex_unlock(&Battery_lock);
  148. if (t_ret)
  149. mtktsbattery_dprintk("[mtktsbattery_get_hw_temp] counter=%d, first_time =%d\n",
  150. counter, first_time);
  151. mtktsbattery_dprintk("[mtktsbattery_get_hw_temp] T_Battery, %d\n", t_ret);
  152. return t_ret;
  153. }
  154. static int mtktsbattery_get_temp(struct thermal_zone_device *thermal, unsigned long *t)
  155. {
  156. *t = mtktsbattery_get_hw_temp();
  157. if ((int)*t >= polling_trip_temp1)
  158. thermal->polling_delay = interval * 1000;
  159. else if ((int)*t < polling_trip_temp2)
  160. thermal->polling_delay = interval * polling_factor2;
  161. else
  162. thermal->polling_delay = interval * polling_factor1;
  163. return 0;
  164. }
  165. static int mtktsbattery_bind(struct thermal_zone_device *thermal,
  166. struct thermal_cooling_device *cdev)
  167. {
  168. int table_val = 0;
  169. if (!strcmp(cdev->type, g_bind0)) {
  170. table_val = 0;
  171. mtktsbattery_dprintk("[mtktsbattery_bind] %s\n", cdev->type);
  172. } else if (!strcmp(cdev->type, g_bind1)) {
  173. table_val = 1;
  174. mtktsbattery_dprintk("[mtktsbattery_bind] %s\n", cdev->type);
  175. } else if (!strcmp(cdev->type, g_bind2)) {
  176. table_val = 2;
  177. mtktsbattery_dprintk("[mtktsbattery_bind] %s\n", cdev->type);
  178. } else if (!strcmp(cdev->type, g_bind3)) {
  179. table_val = 3;
  180. mtktsbattery_dprintk("[mtktsbattery_bind] %s\n", cdev->type);
  181. } else if (!strcmp(cdev->type, g_bind4)) {
  182. table_val = 4;
  183. mtktsbattery_dprintk("[mtktsbattery_bind] %s\n", cdev->type);
  184. } else if (!strcmp(cdev->type, g_bind5)) {
  185. table_val = 5;
  186. mtktsbattery_dprintk("[mtktsbattery_bind] %s\n", cdev->type);
  187. } else if (!strcmp(cdev->type, g_bind6)) {
  188. table_val = 6;
  189. mtktsbattery_dprintk("[mtktsbattery_bind] %s\n", cdev->type);
  190. } else if (!strcmp(cdev->type, g_bind7)) {
  191. table_val = 7;
  192. mtktsbattery_dprintk("[mtktsbattery_bind] %s\n", cdev->type);
  193. } else if (!strcmp(cdev->type, g_bind8)) {
  194. table_val = 8;
  195. mtktsbattery_dprintk("[mtktsbattery_bind] %s\n", cdev->type);
  196. } else if (!strcmp(cdev->type, g_bind9)) {
  197. table_val = 9;
  198. mtktsbattery_dprintk("[mtktsbattery_bind] %s\n", cdev->type);
  199. } else {
  200. return 0;
  201. }
  202. if (mtk_thermal_zone_bind_cooling_device(thermal, table_val, cdev)) {
  203. mtktsbattery_dprintk("[mtktsbattery_bind] error binding cooling dev\n");
  204. return -EINVAL;
  205. }
  206. mtktsbattery_dprintk("[mtktsbattery_bind] binding OK, %d\n", table_val);
  207. return 0;
  208. }
  209. static int mtktsbattery_unbind(struct thermal_zone_device *thermal,
  210. struct thermal_cooling_device *cdev)
  211. {
  212. int table_val = 0;
  213. if (!strcmp(cdev->type, g_bind0)) {
  214. table_val = 0;
  215. mtktsbattery_dprintk("[mtktsbattery_unbind] %s\n", cdev->type);
  216. } else if (!strcmp(cdev->type, g_bind1)) {
  217. table_val = 1;
  218. mtktsbattery_dprintk("[mtktsbattery_unbind] %s\n", cdev->type);
  219. } else if (!strcmp(cdev->type, g_bind2)) {
  220. table_val = 2;
  221. mtktsbattery_dprintk("[mtktsbattery_unbind] %s\n", cdev->type);
  222. } else if (!strcmp(cdev->type, g_bind3)) {
  223. table_val = 3;
  224. mtktsbattery_dprintk("[mtktsbattery_unbind] %s\n", cdev->type);
  225. } else if (!strcmp(cdev->type, g_bind4)) {
  226. table_val = 4;
  227. mtktsbattery_dprintk("[mtktsbattery_unbind] %s\n", cdev->type);
  228. } else if (!strcmp(cdev->type, g_bind5)) {
  229. table_val = 5;
  230. mtktsbattery_dprintk("[mtktsbattery_unbind] %s\n", cdev->type);
  231. } else if (!strcmp(cdev->type, g_bind6)) {
  232. table_val = 6;
  233. mtktsbattery_dprintk("[mtktsbattery_unbind] %s\n", cdev->type);
  234. } else if (!strcmp(cdev->type, g_bind7)) {
  235. table_val = 7;
  236. mtktsbattery_dprintk("[mtktsbattery_unbind] %s\n", cdev->type);
  237. } else if (!strcmp(cdev->type, g_bind8)) {
  238. table_val = 8;
  239. mtktsbattery_dprintk("[mtktsbattery_unbind] %s\n", cdev->type);
  240. } else if (!strcmp(cdev->type, g_bind9)) {
  241. table_val = 9;
  242. mtktsbattery_dprintk("[mtktsbattery_unbind] %s\n", cdev->type);
  243. } else
  244. return 0;
  245. if (thermal_zone_unbind_cooling_device(thermal, table_val, cdev)) {
  246. mtktsbattery_dprintk("[mtktsbattery_unbind] error unbinding cooling dev\n");
  247. return -EINVAL;
  248. }
  249. mtktsbattery_dprintk("[mtktsbattery_unbind] unbinding OK\n");
  250. return 0;
  251. }
  252. static int mtktsbattery_get_mode(struct thermal_zone_device *thermal,
  253. enum thermal_device_mode *mode)
  254. {
  255. *mode = (kernelmode) ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED;
  256. return 0;
  257. }
  258. static int mtktsbattery_set_mode(struct thermal_zone_device *thermal, enum thermal_device_mode mode)
  259. {
  260. kernelmode = mode;
  261. return 0;
  262. }
  263. static int mtktsbattery_get_trip_type(struct thermal_zone_device *thermal, int trip,
  264. enum thermal_trip_type *type)
  265. {
  266. *type = g_THERMAL_TRIP[trip];
  267. return 0;
  268. }
  269. static int mtktsbattery_get_trip_temp(struct thermal_zone_device *thermal, int trip,
  270. unsigned long *temp)
  271. {
  272. *temp = trip_temp[trip];
  273. return 0;
  274. }
  275. static int mtktsbattery_get_crit_temp(struct thermal_zone_device *thermal,
  276. unsigned long *temperature)
  277. {
  278. *temperature = mtktsbattery_TEMP_CRIT;
  279. return 0;
  280. }
  281. /* bind callback functions to thermalzone */
  282. static struct thermal_zone_device_ops mtktsbattery_dev_ops = {
  283. .bind = mtktsbattery_bind,
  284. .unbind = mtktsbattery_unbind,
  285. .get_temp = mtktsbattery_get_temp,
  286. .get_mode = mtktsbattery_get_mode,
  287. .set_mode = mtktsbattery_set_mode,
  288. .get_trip_type = mtktsbattery_get_trip_type,
  289. .get_trip_temp = mtktsbattery_get_trip_temp,
  290. .get_crit_temp = mtktsbattery_get_crit_temp,
  291. };
  292. /*
  293. static int dis_charge_get_max_state(struct thermal_cooling_device *cdev,
  294. unsigned long *state)
  295. {
  296. *state = 1;
  297. return 0;
  298. }
  299. static int dis_charge_get_cur_state(struct thermal_cooling_device *cdev,
  300. unsigned long *state)
  301. {
  302. *state = cl_dev_dis_charge_state;
  303. return 0;
  304. }
  305. static int dis_charge_set_cur_state(struct thermal_cooling_device *cdev,
  306. unsigned long state)
  307. {
  308. cl_dev_dis_charge_state = state;
  309. if(cl_dev_dis_charge_state == 1) {
  310. mtktsbattery_dprintk("[dis_charge_set_cur_state] disable charging\n");
  311. }
  312. return 0;
  313. }
  314. */
  315. static int tsbat_sysrst_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
  316. {
  317. *state = 1;
  318. return 0;
  319. }
  320. static int tsbat_sysrst_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
  321. {
  322. *state = cl_dev_sysrst_state;
  323. return 0;
  324. }
  325. static int tsbat_sysrst_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  326. {
  327. cl_dev_sysrst_state = state;
  328. if (cl_dev_sysrst_state == 1) {
  329. pr_debug("Power/battery_Thermal: reset, reset, reset!!!");
  330. pr_debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  331. pr_debug("*****************************************");
  332. pr_debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  333. /* BUG(); */
  334. /* arch_reset(0,NULL); */
  335. *(unsigned int *)0x0 = 0xdead; /* To trigger data abort to reset the system for thermal protection. */
  336. }
  337. return 0;
  338. }
  339. /*
  340. static struct thermal_cooling_device_ops mtktsbattery_cooling_dis_charge_ops = {
  341. .get_max_state = dis_charge_get_max_state,
  342. .get_cur_state = dis_charge_get_cur_state,
  343. .set_cur_state = dis_charge_set_cur_state,
  344. };*/
  345. static struct thermal_cooling_device_ops mtktsbattery_cooling_sysrst_ops = {
  346. .get_max_state = tsbat_sysrst_get_max_state,
  347. .get_cur_state = tsbat_sysrst_get_cur_state,
  348. .set_cur_state = tsbat_sysrst_set_cur_state,
  349. };
  350. static int mtktsbattery_read(struct seq_file *m, void *v)
  351. /* static int mtktsbattery_read(char *buf, char **start, off_t off, int count, int *eof, void *data) */
  352. {
  353. seq_printf(m, "[mtktsbattery_read] trip_0_temp=%d,trip_1_temp=%d,trip_2_temp=%d,trip_3_temp=%d,\n",
  354. trip_temp[0], trip_temp[1], trip_temp[2], trip_temp[3]);
  355. seq_printf(m, "trip_4_temp=%d,trip_5_temp=%d,trip_6_temp=%d,trip_7_temp=%d,trip_8_temp=%d,trip_9_temp=%d,\n",
  356. trip_temp[4], trip_temp[5], trip_temp[6], trip_temp[7], trip_temp[8], trip_temp[9]);
  357. seq_printf(m, "g_THERMAL_TRIP_0=%d,g_THERMAL_TRIP_1=%d,g_THERMAL_TRIP_2=%d,g_THERMAL_TRIP_3=%d,",
  358. g_THERMAL_TRIP[0], g_THERMAL_TRIP[1], g_THERMAL_TRIP[2], g_THERMAL_TRIP[3]);
  359. seq_printf(m, "g_THERMAL_TRIP_4=%d,g_THERMAL_TRIP_5=%d,g_THERMAL_TRIP_6=%d,g_THERMAL_TRIP_7=%d,",
  360. g_THERMAL_TRIP[4], g_THERMAL_TRIP[5], g_THERMAL_TRIP[6], g_THERMAL_TRIP[7]);
  361. seq_printf(m, "g_THERMAL_TRIP_8=%d,g_THERMAL_TRIP_9=%d,\n", g_THERMAL_TRIP[8], g_THERMAL_TRIP[9]);
  362. seq_printf(m, "cooldev0=%s,cooldev1=%s,cooldev2=%s,cooldev3=%s,cooldev4=%s,\n",
  363. g_bind0, g_bind1, g_bind2, g_bind3, g_bind4);
  364. seq_printf(m, "cooldev5=%s,cooldev6=%s,cooldev7=%s,cooldev8=%s,cooldev9=%s,time_ms=%d\n",
  365. g_bind5, g_bind6, g_bind7, g_bind8, g_bind9, interval * 1000);
  366. return 0;
  367. }
  368. static int mtktsbattery_register_thermal(void);
  369. static void mtktsbattery_unregister_thermal(void);
  370. static ssize_t mtktsbattery_write(struct file *file, const char __user *buffer, size_t count,
  371. loff_t *data)
  372. /* static ssize_t mtktsbattery_write(struct file *file, const char *buffer, unsigned long count, void *data) */
  373. {
  374. int len = 0, i;
  375. struct mtktsbattery_data {
  376. int trip[10];
  377. int t_type[10];
  378. char bind0[20], bind1[20], bind2[20], bind3[20], bind4[20];
  379. char bind5[20], bind6[20], bind7[20], bind8[20], bind9[20];
  380. int time_msec;
  381. char desc[512];
  382. };
  383. struct mtktsbattery_data *ptr_mtktsbattery_data = kmalloc(sizeof(*ptr_mtktsbattery_data), GFP_KERNEL);
  384. if (ptr_mtktsbattery_data == NULL)
  385. return -ENOMEM;
  386. len = (count < (sizeof(ptr_mtktsbattery_data->desc) - 1)) ? count : (sizeof(ptr_mtktsbattery_data->desc) - 1);
  387. if (copy_from_user(ptr_mtktsbattery_data->desc, buffer, len)) {
  388. kfree(ptr_mtktsbattery_data);
  389. return 0;
  390. }
  391. ptr_mtktsbattery_data->desc[len] = '\0';
  392. if (sscanf
  393. (ptr_mtktsbattery_data->desc,
  394. "%d %d %d %s %d %d %s %d %d %s %d %d %s %d %d %s %d %d %s %d %d %s %d %d %s %d %d %s %d %d %s %d",
  395. &num_trip,
  396. &ptr_mtktsbattery_data->trip[0], &ptr_mtktsbattery_data->t_type[0], ptr_mtktsbattery_data->bind0,
  397. &ptr_mtktsbattery_data->trip[1], &ptr_mtktsbattery_data->t_type[1], ptr_mtktsbattery_data->bind1,
  398. &ptr_mtktsbattery_data->trip[2], &ptr_mtktsbattery_data->t_type[2], ptr_mtktsbattery_data->bind2,
  399. &ptr_mtktsbattery_data->trip[3], &ptr_mtktsbattery_data->t_type[3], ptr_mtktsbattery_data->bind3,
  400. &ptr_mtktsbattery_data->trip[4], &ptr_mtktsbattery_data->t_type[4], ptr_mtktsbattery_data->bind4,
  401. &ptr_mtktsbattery_data->trip[5], &ptr_mtktsbattery_data->t_type[5], ptr_mtktsbattery_data->bind5,
  402. &ptr_mtktsbattery_data->trip[6], &ptr_mtktsbattery_data->t_type[6], ptr_mtktsbattery_data->bind6,
  403. &ptr_mtktsbattery_data->trip[7], &ptr_mtktsbattery_data->t_type[7], ptr_mtktsbattery_data->bind7,
  404. &ptr_mtktsbattery_data->trip[8], &ptr_mtktsbattery_data->t_type[8], ptr_mtktsbattery_data->bind8,
  405. &ptr_mtktsbattery_data->trip[9], &ptr_mtktsbattery_data->t_type[9], ptr_mtktsbattery_data->bind9,
  406. &ptr_mtktsbattery_data->time_msec) == 32) {
  407. mtktsbattery_dprintk("[mtktsbattery_write] mtktsbattery_unregister_thermal\n");
  408. mtktsbattery_unregister_thermal();
  409. if (num_trip < 0 || num_trip > 10) {
  410. aee_kernel_warning_api(__FILE__, __LINE__, DB_OPT_DEFAULT, "mtktsbattery_write",
  411. "Bad argument");
  412. mtktsbattery_dprintk("[mtktsbattery_write] bad argument\n");
  413. kfree(ptr_mtktsbattery_data);
  414. return -EINVAL;
  415. }
  416. for (i = 0; i < num_trip; i++)
  417. g_THERMAL_TRIP[i] = ptr_mtktsbattery_data->t_type[i];
  418. g_bind0[0] = g_bind1[0] = g_bind2[0] = g_bind3[0] = g_bind4[0] = g_bind5[0] =
  419. g_bind6[0] = g_bind7[0] = g_bind8[0] = g_bind9[0] = '\0';
  420. for (i = 0; i < 20; i++) {
  421. g_bind0[i] = ptr_mtktsbattery_data->bind0[i];
  422. g_bind1[i] = ptr_mtktsbattery_data->bind1[i];
  423. g_bind2[i] = ptr_mtktsbattery_data->bind2[i];
  424. g_bind3[i] = ptr_mtktsbattery_data->bind3[i];
  425. g_bind4[i] = ptr_mtktsbattery_data->bind4[i];
  426. g_bind5[i] = ptr_mtktsbattery_data->bind5[i];
  427. g_bind6[i] = ptr_mtktsbattery_data->bind6[i];
  428. g_bind7[i] = ptr_mtktsbattery_data->bind7[i];
  429. g_bind8[i] = ptr_mtktsbattery_data->bind8[i];
  430. g_bind9[i] = ptr_mtktsbattery_data->bind9[i];
  431. }
  432. mtktsbattery_dprintk("[mtktsbattery_write] g_THERMAL_TRIP_0=%d,g_THERMAL_TRIP_1=%d,",
  433. g_THERMAL_TRIP[0], g_THERMAL_TRIP[1]);
  434. mtktsbattery_dprintk("g_THERMAL_TRIP_2=%d,g_THERMAL_TRIP_3=%d,g_THERMAL_TRIP_4=%d,",
  435. g_THERMAL_TRIP[2], g_THERMAL_TRIP[3], g_THERMAL_TRIP[4]);
  436. mtktsbattery_dprintk("g_THERMAL_TRIP_5=%d,g_THERMAL_TRIP_6=%d,g_THERMAL_TRIP_7=%d,",
  437. g_THERMAL_TRIP[5], g_THERMAL_TRIP[6], g_THERMAL_TRIP[7]);
  438. mtktsbattery_dprintk("g_THERMAL_TRIP_8=%d,g_THERMAL_TRIP_9=%d,\n",
  439. g_THERMAL_TRIP[8], g_THERMAL_TRIP[9]);
  440. mtktsbattery_dprintk("[mtktsbattery_write] cooldev0=%s,cooldev1=%s,cooldev2=%s,cooldev3=%s,",
  441. g_bind0, g_bind1, g_bind2, g_bind3);
  442. mtktsbattery_dprintk("cooldev4=%s,cooldev5=%s,cooldev6=%s,cooldev7=%s,cooldev8=%s,cooldev9=%s\n",
  443. g_bind4, g_bind5, g_bind6, g_bind7, g_bind8, g_bind9);
  444. for (i = 0; i < num_trip; i++)
  445. trip_temp[i] = ptr_mtktsbattery_data->trip[i];
  446. interval = ptr_mtktsbattery_data->time_msec / 1000;
  447. mtktsbattery_dprintk("[mtktsbattery_write] trip_0_temp=%d,trip_1_temp=%d,trip_2_temp=%d,",
  448. trip_temp[0], trip_temp[1], trip_temp[2]);
  449. mtktsbattery_dprintk("trip_3_temp=%d,trip_4_temp=%d,trip_5_temp=%d,trip_6_temp=%d,trip_7_temp=%d,",
  450. trip_temp[3], trip_temp[4], trip_temp[5], trip_temp[6], trip_temp[7]);
  451. mtktsbattery_dprintk("trip_8_temp=%d,trip_9_temp=%d,time_ms=%d\n",
  452. trip_temp[8], trip_temp[9], interval * 1000);
  453. mtktsbattery_dprintk("[mtktsbattery_write] mtktsbattery_register_thermal\n");
  454. mtktsbattery_register_thermal();
  455. kfree(ptr_mtktsbattery_data);
  456. /* battery_write_flag=1; */
  457. return count;
  458. }
  459. mtktsbattery_dprintk("[mtktsbattery_write] bad argument\n");
  460. aee_kernel_warning_api(__FILE__, __LINE__, DB_OPT_DEFAULT, "mtktsbattery_write",
  461. "Bad argument");
  462. kfree(ptr_mtktsbattery_data);
  463. return -EINVAL;
  464. }
  465. void mtkts_battery_cancel_thermal_timer(void)
  466. {
  467. /* cancel timer */
  468. /*pr_debug("mtkts_battery_cancel_thermal_timer\n"); */
  469. /* stop thermal framework polling when entering deep idle */
  470. /* For charging current throttling during deep idle,
  471. this delayed work cannot be canceled.
  472. if (thz_dev)
  473. cancel_delayed_work(&(thz_dev->poll_queue));
  474. */
  475. return;
  476. }
  477. void mtkts_battery_start_thermal_timer(void)
  478. {
  479. /*pr_debug("mtkts_battery_start_thermal_timer\n"); */
  480. /* resume thermal framework polling when leaving deep idle */
  481. /* For charging current throttling during deep idle,
  482. this delayed work cannot be canceled.
  483. if (thz_dev != NULL && interval != 0)
  484. mod_delayed_work(system_freezable_wq, &(thz_dev->poll_queue), round_jiffies(msecs_to_jiffies(3000)));
  485. */
  486. return;
  487. }
  488. int mtktsbattery_register_cooler(void)
  489. {
  490. /* cooling devices */
  491. cl_dev_sysrst = mtk_thermal_cooling_device_register("mtktsbattery-sysrst", NULL,
  492. &mtktsbattery_cooling_sysrst_ops);
  493. return 0;
  494. }
  495. static int mtktsbattery_register_thermal(void)
  496. {
  497. mtktsbattery_dprintk("[mtktsbattery_register_thermal]\n");
  498. /* trips : trip 0~1 */
  499. thz_dev = mtk_thermal_zone_device_register("mtktsbattery", num_trip, NULL,
  500. &mtktsbattery_dev_ops, 0, 0, 0, interval * 1000);
  501. return 0;
  502. }
  503. void mtktsbattery_unregister_cooler(void)
  504. {
  505. if (cl_dev_sysrst) {
  506. mtk_thermal_cooling_device_unregister(cl_dev_sysrst);
  507. cl_dev_sysrst = NULL;
  508. }
  509. }
  510. static void mtktsbattery_unregister_thermal(void)
  511. {
  512. mtktsbattery_dprintk("[mtktsbattery_unregister_thermal]\n");
  513. if (thz_dev) {
  514. mtk_thermal_zone_device_unregister(thz_dev);
  515. thz_dev = NULL;
  516. }
  517. }
  518. static int mtkts_battery_open(struct inode *inode, struct file *file)
  519. {
  520. return single_open(file, mtktsbattery_read, NULL);
  521. }
  522. static const struct file_operations mtkts_battery_fops = {
  523. .owner = THIS_MODULE,
  524. .open = mtkts_battery_open,
  525. .read = seq_read,
  526. .llseek = seq_lseek,
  527. .write = mtktsbattery_write,
  528. .release = single_release,
  529. };
  530. static int __init mtktsbattery_init(void)
  531. {
  532. int err = 0;
  533. struct proc_dir_entry *entry = NULL;
  534. struct proc_dir_entry *mtktsbattery_dir = NULL;
  535. mtktsbattery_dprintk("[mtktsbattery_init]\n");
  536. err = mtktsbattery_register_cooler();
  537. if (err)
  538. return err;
  539. err = mtktsbattery_register_thermal();
  540. if (err)
  541. goto err_unreg;
  542. mtktsbattery_dir = mtk_thermal_get_proc_drv_therm_dir_entry();
  543. if (!mtktsbattery_dir) {
  544. mtktsbattery_dprintk("%s mkdir /proc/driver/thermal failed\n", __func__);
  545. } else {
  546. entry =
  547. proc_create("tzbattery", S_IRUGO | S_IWUSR | S_IWGRP, mtktsbattery_dir,
  548. &mtkts_battery_fops);
  549. if (entry)
  550. proc_set_user(entry, uid, gid);
  551. }
  552. return 0;
  553. err_unreg:
  554. mtktsbattery_unregister_cooler();
  555. return err;
  556. }
  557. static void __exit mtktsbattery_exit(void)
  558. {
  559. mtktsbattery_dprintk("[mtktsbattery_exit]\n");
  560. mtktsbattery_unregister_thermal();
  561. mtktsbattery_unregister_cooler();
  562. }
  563. module_init(mtktsbattery_init);
  564. module_exit(mtktsbattery_exit);