ts_da9214.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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/seq_file.h>
  13. #include "mt-plat/mtk_thermal_monitor.h"
  14. #include "mtk_thermal_typedefs.h"
  15. #include "mach/mt_thermal.h"
  16. #include "da9214.h"
  17. #include <linux/uidgid.h>
  18. #include <linux/slab.h>
  19. static kuid_t uid = KUIDT_INIT(0);
  20. static kgid_t gid = KGIDT_INIT(1000);
  21. static unsigned int interval; /* seconds, 0 : no auto polling */
  22. static int trip_temp[10] = { 125000, 110000, 100000, 90000, 80000, 70000, 65000, 60000, 55000, 50000 };
  23. static int pre_temp = 60000;
  24. static unsigned int cl_dev_sysrst_state;
  25. static struct thermal_zone_device *thz_dev;
  26. static struct thermal_cooling_device *cl_dev_sysrst;
  27. static int tsda9214_debug_log;
  28. static int kernelmode;
  29. static int g_THERMAL_TRIP[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  30. static int num_trip;
  31. static char g_bind0[20] = "tsda9214-sysrst";
  32. static char g_bind1[20] = "cpu01";
  33. static char g_bind2[20] = "";
  34. static char g_bind3[20] = "";
  35. static char g_bind4[20] = "";
  36. static char g_bind5[20] = "";
  37. static char g_bind6[20] = "";
  38. static char g_bind7[20] = "";
  39. static char g_bind8[20] = "";
  40. static char g_bind9[20] = "";
  41. #define tsda9214_TEMP_CRIT 150000 /* 150.000 degree Celsius */
  42. #define tsda9214_dprintk(fmt, args...) \
  43. do { \
  44. if (tsda9214_debug_log) \
  45. pr_debug("[Power/da9214_Thermal]" fmt, ##args); \
  46. } while (0)
  47. static int tsda9214_get_temp(struct thermal_zone_device *thermal, unsigned long *t)
  48. {
  49. unsigned char val = 0;
  50. tsda9214_dprintk("[tsda9214_get_temp]\n");
  51. if (da9214_read_interface(0x51, &val, 0x11, 2) == 1) {
  52. switch (val) {
  53. case 0x00:
  54. /* < 125 */
  55. *t = 60000;
  56. break;
  57. case 0x01:
  58. /* 125 ~ 140 */
  59. *t = 125000;
  60. break;
  61. case 0x10:
  62. case 0x11:
  63. /* 140 ~ 150 */
  64. *t = 140000;
  65. break;
  66. default:
  67. tsda9214_dprintk("Error, use the previous temperature\n");
  68. *t = pre_temp;
  69. }
  70. } else {
  71. tsda9214_dprintk("Error, use the previous temperature\n");
  72. *t = pre_temp;
  73. }
  74. pre_temp = *t;
  75. tsda9214_dprintk("temp =%lu\n", *t);
  76. return 0;
  77. }
  78. static int tsda9214_bind(struct thermal_zone_device *thermal, struct thermal_cooling_device *cdev)
  79. {
  80. int table_val = 0;
  81. if (!strcmp(cdev->type, g_bind0))
  82. table_val = 0;
  83. else if (!strcmp(cdev->type, g_bind1))
  84. table_val = 1;
  85. else if (!strcmp(cdev->type, g_bind2))
  86. table_val = 2;
  87. else if (!strcmp(cdev->type, g_bind3))
  88. table_val = 3;
  89. else if (!strcmp(cdev->type, g_bind4))
  90. table_val = 4;
  91. else if (!strcmp(cdev->type, g_bind5))
  92. table_val = 5;
  93. else if (!strcmp(cdev->type, g_bind6))
  94. table_val = 6;
  95. else if (!strcmp(cdev->type, g_bind7))
  96. table_val = 7;
  97. else if (!strcmp(cdev->type, g_bind8))
  98. table_val = 8;
  99. else if (!strcmp(cdev->type, g_bind9))
  100. table_val = 9;
  101. else
  102. return 0;
  103. if (mtk_thermal_zone_bind_cooling_device(thermal, table_val, cdev)) {
  104. tsda9214_dprintk("[tsda9214_bind] error binding cooling dev\n");
  105. return -EINVAL;
  106. }
  107. tsda9214_dprintk("[tsda9214_bind] binding OK, %d\n", table_val);
  108. return 0;
  109. }
  110. static int tsda9214_unbind(struct thermal_zone_device *thermal,
  111. struct thermal_cooling_device *cdev)
  112. {
  113. int table_val = 0;
  114. if (!strcmp(cdev->type, g_bind0))
  115. table_val = 0;
  116. else if (!strcmp(cdev->type, g_bind1))
  117. table_val = 1;
  118. else if (!strcmp(cdev->type, g_bind2))
  119. table_val = 2;
  120. else if (!strcmp(cdev->type, g_bind3))
  121. table_val = 3;
  122. else if (!strcmp(cdev->type, g_bind4))
  123. table_val = 4;
  124. else if (!strcmp(cdev->type, g_bind5))
  125. table_val = 5;
  126. else if (!strcmp(cdev->type, g_bind6))
  127. table_val = 6;
  128. else if (!strcmp(cdev->type, g_bind7))
  129. table_val = 7;
  130. else if (!strcmp(cdev->type, g_bind8))
  131. table_val = 8;
  132. else if (!strcmp(cdev->type, g_bind9))
  133. table_val = 9;
  134. else
  135. return 0;
  136. if (thermal_zone_unbind_cooling_device(thermal, table_val, cdev)) {
  137. tsda9214_dprintk("[tsda9214_unbind] error unbinding cooling dev\n");
  138. return -EINVAL;
  139. }
  140. tsda9214_dprintk("[tsda9214_unbind] unbinding OK\n");
  141. return 0;
  142. }
  143. static int tsda9214_get_mode(struct thermal_zone_device *thermal, enum thermal_device_mode *mode)
  144. {
  145. *mode = (kernelmode) ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED;
  146. return 0;
  147. }
  148. static int tsda9214_set_mode(struct thermal_zone_device *thermal, enum thermal_device_mode mode)
  149. {
  150. kernelmode = mode;
  151. return 0;
  152. }
  153. static int tsda9214_get_trip_type(struct thermal_zone_device *thermal, int trip,
  154. enum thermal_trip_type *type)
  155. {
  156. *type = g_THERMAL_TRIP[trip];
  157. return 0;
  158. }
  159. static int tsda9214_get_trip_temp(struct thermal_zone_device *thermal, int trip,
  160. unsigned long *temp)
  161. {
  162. *temp = trip_temp[trip];
  163. return 0;
  164. }
  165. static int tsda9214_get_crit_temp(struct thermal_zone_device *thermal, unsigned long *temperature)
  166. {
  167. *temperature = tsda9214_TEMP_CRIT;
  168. return 0;
  169. }
  170. /* bind callback functions to thermalzone */
  171. static struct thermal_zone_device_ops tsda9214_dev_ops = {
  172. .bind = tsda9214_bind,
  173. .unbind = tsda9214_unbind,
  174. .get_temp = tsda9214_get_temp,
  175. .get_mode = tsda9214_get_mode,
  176. .set_mode = tsda9214_set_mode,
  177. .get_trip_type = tsda9214_get_trip_type,
  178. .get_trip_temp = tsda9214_get_trip_temp,
  179. .get_crit_temp = tsda9214_get_crit_temp,
  180. };
  181. static int tsda9214_sysrst_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
  182. {
  183. tsda9214_dprintk("tsda9214_sysrst_get_max_state!!!\n");
  184. *state = 1;
  185. return 0;
  186. }
  187. static int tsda9214_sysrst_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
  188. {
  189. tsda9214_dprintk("tsda9214_sysrst_get_cur_state = %d\n", cl_dev_sysrst_state);
  190. *state = cl_dev_sysrst_state;
  191. return 0;
  192. }
  193. static int tsda9214_sysrst_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  194. {
  195. tsda9214_dprintk("tsda9214_sysrst_set_cur_state = %d\n", cl_dev_sysrst_state);
  196. cl_dev_sysrst_state = state;
  197. if (cl_dev_sysrst_state == 1) {
  198. pr_debug("Power/da9214_Thermal: reset, reset, reset!!!");
  199. pr_debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  200. pr_debug("*****************************************");
  201. pr_debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  202. #ifndef CONFIG_ARM64
  203. BUG();
  204. #else
  205. *(unsigned int *)0x0 = 0xdead; /* To trigger data abort to reset the system for thermal protection. */
  206. #endif
  207. }
  208. return 0;
  209. }
  210. static struct thermal_cooling_device_ops tsda9214_cooling_sysrst_ops = {
  211. .get_max_state = tsda9214_sysrst_get_max_state,
  212. .get_cur_state = tsda9214_sysrst_get_cur_state,
  213. .set_cur_state = tsda9214_sysrst_set_cur_state,
  214. };
  215. int tsda9214_register_cooler(void)
  216. {
  217. cl_dev_sysrst = mtk_thermal_cooling_device_register("tsda9214-sysrst", NULL,
  218. &tsda9214_cooling_sysrst_ops);
  219. return 0;
  220. }
  221. static int tsda9214_read(struct seq_file *m, void *v)
  222. {
  223. seq_printf(m, "[tsda9214_read] trip_0_temp=%d,trip_1_temp=%d,trip_2_temp=%d,trip_3_temp=%d,trip_4_temp=%d,\n",
  224. trip_temp[0], trip_temp[1], trip_temp[2], trip_temp[3], trip_temp[4]);
  225. seq_printf(m, "trip_5_temp=%d,trip_6_temp=%d,trip_7_temp=%d,trip_8_temp=%d,trip_9_temp=%d,\n",
  226. trip_temp[5], trip_temp[6], trip_temp[7], trip_temp[8], trip_temp[9]);
  227. seq_printf(m, "g_THERMAL_TRIP_0=%d,g_THERMAL_TRIP_1=%d,g_THERMAL_TRIP_2=%d,g_THERMAL_TRIP_3=%d\n",
  228. g_THERMAL_TRIP[0], g_THERMAL_TRIP[1], g_THERMAL_TRIP[2], g_THERMAL_TRIP[3]);
  229. seq_printf(m, "g_THERMAL_TRIP_4=%d, g_THERMAL_TRIP_5=%d,g_THERMAL_TRIP_6=%d,g_THERMAL_TRIP_7=%d\n",
  230. g_THERMAL_TRIP[4], g_THERMAL_TRIP[5], g_THERMAL_TRIP[6], g_THERMAL_TRIP[7]);
  231. seq_printf(m, "g_THERMAL_TRIP_8=%d,g_THERMAL_TRIP_9=%d,\n",
  232. g_THERMAL_TRIP[8], g_THERMAL_TRIP[9]);
  233. seq_printf(m, "cooldev0=%s,cooldev1=%s,cooldev2=%s,cooldev3=%s,cooldev4=%s,\n",
  234. g_bind0, g_bind1, g_bind2, g_bind3, g_bind4);
  235. seq_printf(m, "cooldev5=%s,cooldev6=%s,cooldev7=%s,cooldev8=%s,cooldev9=%s,time_ms=%d\n",
  236. g_bind5, g_bind6, g_bind7, g_bind8, g_bind9, interval * 1000);
  237. return 0;
  238. }
  239. static int tsda9214_register_thermal(void);
  240. static void tsda9214_unregister_thermal(void);
  241. static ssize_t tsda9214_write(struct file *file, const char __user *buffer, size_t count,
  242. loff_t *data)
  243. {
  244. int len = 0, i;
  245. struct tsda9214_data {
  246. int trip[10];
  247. int t_type[10];
  248. char bind0[20], bind1[20], bind2[20], bind3[20], bind4[20];
  249. char bind5[20], bind6[20], bind7[20], bind8[20], bind9[20];
  250. int time_msec;
  251. char desc[512];
  252. };
  253. struct tsda9214_data *ptr_tsda9214_data = kmalloc(sizeof(*ptr_tsda9214_data), GFP_KERNEL);
  254. if (ptr_tsda9214_data == NULL)
  255. return -ENOMEM;
  256. len = (count < (sizeof(ptr_tsda9214_data->desc) - 1)) ? count : (sizeof(ptr_tsda9214_data->desc) - 1);
  257. if (copy_from_user(ptr_tsda9214_data->desc, buffer, len)) {
  258. kfree(ptr_tsda9214_data);
  259. return 0;
  260. }
  261. ptr_tsda9214_data->desc[len] = '\0';
  262. if (sscanf
  263. (ptr_tsda9214_data->desc,
  264. "%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",
  265. &num_trip,
  266. &ptr_tsda9214_data->trip[0], &ptr_tsda9214_data->t_type[0], ptr_tsda9214_data->bind0,
  267. &ptr_tsda9214_data->trip[1], &ptr_tsda9214_data->t_type[1], ptr_tsda9214_data->bind1,
  268. &ptr_tsda9214_data->trip[2], &ptr_tsda9214_data->t_type[2], ptr_tsda9214_data->bind2,
  269. &ptr_tsda9214_data->trip[3], &ptr_tsda9214_data->t_type[3], ptr_tsda9214_data->bind3,
  270. &ptr_tsda9214_data->trip[4], &ptr_tsda9214_data->t_type[4], ptr_tsda9214_data->bind4,
  271. &ptr_tsda9214_data->trip[5], &ptr_tsda9214_data->t_type[5], ptr_tsda9214_data->bind5,
  272. &ptr_tsda9214_data->trip[6], &ptr_tsda9214_data->t_type[6], ptr_tsda9214_data->bind6,
  273. &ptr_tsda9214_data->trip[7], &ptr_tsda9214_data->t_type[7], ptr_tsda9214_data->bind7,
  274. &ptr_tsda9214_data->trip[8], &ptr_tsda9214_data->t_type[8], ptr_tsda9214_data->bind8,
  275. &ptr_tsda9214_data->trip[9], &ptr_tsda9214_data->t_type[9], ptr_tsda9214_data->bind9,
  276. &ptr_tsda9214_data->time_msec) == 32) {
  277. tsda9214_dprintk("[tsda9214_write] tsda9214_unregister_thermal\n");
  278. tsda9214_unregister_thermal();
  279. for (i = 0; i < num_trip; i++)
  280. g_THERMAL_TRIP[i] = ptr_tsda9214_data->t_type[i];
  281. g_bind0[0] = g_bind1[0] = g_bind2[0] = g_bind3[0] = g_bind4[0] = g_bind5[0] =
  282. g_bind6[0] = g_bind7[0] = g_bind8[0] = g_bind9[0] = '\0';
  283. for (i = 0; i < 20; i++) {
  284. g_bind0[i] = ptr_tsda9214_data->bind0[i];
  285. g_bind1[i] = ptr_tsda9214_data->bind1[i];
  286. g_bind2[i] = ptr_tsda9214_data->bind2[i];
  287. g_bind3[i] = ptr_tsda9214_data->bind3[i];
  288. g_bind4[i] = ptr_tsda9214_data->bind4[i];
  289. g_bind5[i] = ptr_tsda9214_data->bind5[i];
  290. g_bind6[i] = ptr_tsda9214_data->bind6[i];
  291. g_bind7[i] = ptr_tsda9214_data->bind7[i];
  292. g_bind8[i] = ptr_tsda9214_data->bind8[i];
  293. g_bind9[i] = ptr_tsda9214_data->bind9[i];
  294. }
  295. tsda9214_dprintk("[tsda9214_write] g_THERMAL_TRIP_0=%d,g_THERMAL_TRIP_1=%d,g_THERMAL_TRIP_2=%d,",
  296. g_THERMAL_TRIP[0], g_THERMAL_TRIP[1], g_THERMAL_TRIP[2]);
  297. tsda9214_dprintk("g_THERMAL_TRIP_3=%d,g_THERMAL_TRIP_4=%d,g_THERMAL_TRIP_5=%d,g_THERMAL_TRIP_6=%d",
  298. g_THERMAL_TRIP[3], g_THERMAL_TRIP[4], g_THERMAL_TRIP[5], g_THERMAL_TRIP[6]);
  299. tsda9214_dprintk("g_THERMAL_TRIP_7=%d,g_THERMAL_TRIP_8=%d,g_THERMAL_TRIP_9=%d,\n",
  300. g_THERMAL_TRIP[7], g_THERMAL_TRIP[8], g_THERMAL_TRIP[9]);
  301. tsda9214_dprintk("[tsda9214_write] cooldev0=%s,cooldev1=%s,cooldev2=%s,cooldev3=%s,cooldev4=%s,",
  302. g_bind0, g_bind1, g_bind2, g_bind3, g_bind4);
  303. tsda9214_dprintk("cooldev5=%s,cooldev6=%s,cooldev7=%s,cooldev8=%s,cooldev9=%s\n",
  304. g_bind5, g_bind6, g_bind7, g_bind8, g_bind9);
  305. for (i = 0; i < num_trip; i++)
  306. trip_temp[i] = ptr_tsda9214_data->trip[i];
  307. interval = ptr_tsda9214_data->time_msec / 1000;
  308. tsda9214_dprintk("[tsda9214_write] trip_0_temp=%d,trip_1_temp=%d,trip_2_temp=%d,trip_3_temp=%d,",
  309. trip_temp[0], trip_temp[1], trip_temp[2], trip_temp[3]);
  310. tsda9214_dprintk("trip_4_temp=%d,trip_5_temp=%d,trip_6_temp=%d,trip_7_temp=%d,trip_8_temp=%d,",
  311. trip_temp[4], trip_temp[5], trip_temp[6], trip_temp[7], trip_temp[8]);
  312. tsda9214_dprintk("trip_9_temp=%d,time_ms=%d\n", trip_temp[9], interval * 1000);
  313. tsda9214_dprintk("[tsda9214_write] tsda9214_register_thermal\n");
  314. tsda9214_register_thermal();
  315. kfree(ptr_tsda9214_data);
  316. return count;
  317. }
  318. tsda9214_dprintk("[tsda9214_write] bad argument\n");
  319. kfree(ptr_tsda9214_data);
  320. return -EINVAL;
  321. }
  322. static int tsda9214_register_thermal(void)
  323. {
  324. tsda9214_dprintk("[tsda9214_register_thermal]\n");
  325. /* trips : trip 0~2 */
  326. thz_dev = mtk_thermal_zone_device_register("tsda9214", num_trip, NULL,
  327. &tsda9214_dev_ops, 0, 0, 0, interval * 1000);
  328. return 0;
  329. }
  330. void tsda9214_unregister_cooler(void)
  331. {
  332. if (cl_dev_sysrst) {
  333. mtk_thermal_cooling_device_unregister(cl_dev_sysrst);
  334. cl_dev_sysrst = NULL;
  335. }
  336. }
  337. static void tsda9214_unregister_thermal(void)
  338. {
  339. tsda9214_dprintk("[tsda9214_unregister_thermal]\n");
  340. if (thz_dev) {
  341. mtk_thermal_zone_device_unregister(thz_dev);
  342. thz_dev = NULL;
  343. }
  344. }
  345. static int tsda9214_open(struct inode *inode, struct file *file)
  346. {
  347. return single_open(file, tsda9214_read, NULL);
  348. }
  349. static const struct file_operations tsda9214_fops = {
  350. .owner = THIS_MODULE,
  351. .open = tsda9214_open,
  352. .read = seq_read,
  353. .llseek = seq_lseek,
  354. .write = tsda9214_write,
  355. .release = single_release,
  356. };
  357. static int __init tsda9214_init(void)
  358. {
  359. int err = 0;
  360. struct proc_dir_entry *entry = NULL;
  361. struct proc_dir_entry *tsda9214_dir = NULL;
  362. tsda9214_dprintk("tsda9214_init: Start\n");
  363. /* return 1 means with 6311, else return 0 */
  364. if (is_da9214_exist() == 0) {
  365. tsda9214_dprintk("tsda9214_init: Buck is not exist\n");
  366. return err;
  367. }
  368. err = tsda9214_register_cooler();
  369. if (err)
  370. return err;
  371. err = tsda9214_register_thermal();
  372. if (err)
  373. goto err_unreg;
  374. tsda9214_dir = mtk_thermal_get_proc_drv_therm_dir_entry();
  375. if (!tsda9214_dir) {
  376. tsda9214_dprintk("[%s]: mkdir /proc/driver/thermal failed\n", __func__);
  377. } else {
  378. entry =
  379. proc_create("tzda9214", S_IRUGO | S_IWUSR | S_IWGRP, tsda9214_dir,
  380. &tsda9214_fops);
  381. if (entry)
  382. proc_set_user(entry, uid, gid);
  383. }
  384. return 0;
  385. err_unreg:
  386. tsda9214_unregister_cooler();
  387. return err;
  388. }
  389. static void __exit tsda9214_exit(void)
  390. {
  391. tsda9214_dprintk("[tsda9214_exit]\n");
  392. tsda9214_unregister_thermal();
  393. tsda9214_unregister_cooler();
  394. }
  395. late_initcall(tsda9214_init);
  396. module_exit(tsda9214_exit);