4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/version.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/printk.h>
10 #include <linux/types.h>
11 #include <linux/kobject.h>
12 #include "mach/mtk_thermal_monitor.h"
14 #define MAX_NUM_INSTANCE_MTK_COOLER_SHUTDOWN 3
16 /* #define MTK_COOLER_SHUTDOWN_UEVENT */
17 #define MTK_COOLER_SHUTDOWN_SIGNAL
19 #if defined(MTK_COOLER_SHUTDOWN_SIGNAL)
20 #include <linux/version.h>
21 #include <linux/proc_fs.h>
22 #include <linux/seq_file.h>
23 #include <asm/uaccess.h>
24 #include <linux/pid.h>
25 #include <linux/signal.h>
26 #include <linux/sched.h>
32 #define mtk_cooler_shutdown_dprintk(fmt, args...) \
33 do { pr_debug("thermal/cooler/shutdown " fmt, ##args); } while (0)
35 #define mtk_cooler_shutdown_dprintk(fmt, args...)
38 extern struct proc_dir_entry
* mtk_thermal_get_proc_drv_therm_dir_entry(void);
40 typedef struct _sd_state
{
45 static struct thermal_cooling_device
*cl_shutdown_dev
[MAX_NUM_INSTANCE_MTK_COOLER_SHUTDOWN
] = { 0 };
46 //static unsigned long cl_shutdown_state[MAX_NUM_INSTANCE_MTK_COOLER_SHUTDOWN] = { 0 };
47 static sd_state cl_sd_state
[MAX_NUM_INSTANCE_MTK_COOLER_SHUTDOWN
];
49 #if defined(MTK_COOLER_SHUTDOWN_SIGNAL)
51 static unsigned int tm_pid
;
52 static unsigned int tm_input_pid
;
53 static unsigned int mtk_cl_sd_rst
;
54 static struct task_struct g_task
;
55 static struct task_struct
*pg_task
= &g_task
;
57 static int sd_debouncet
= 1;
58 //static int sd_cnt = 0;
59 static int sd_happened
= 0;
61 static ssize_t
_mtk_cl_sd_rst_write(struct file
*filp
, const char __user
*buf
, size_t len
,
65 char tmp
[MAX_LEN
] = { 0 };
67 len
= (len
< (MAX_LEN
-1)) ? len
: (MAX_LEN
-1);
68 /* write data to the buffer */
69 if (copy_from_user(tmp
, buf
, len
)) {
73 ret
= kstrtouint(tmp
, 10, &mtk_cl_sd_rst
);
78 if (1 == mtk_cl_sd_rst
) {
80 for (i
= MAX_NUM_INSTANCE_MTK_COOLER_SHUTDOWN
; i
-- > 0;) {
81 cl_sd_state
[i
].state
= 0;
82 cl_sd_state
[i
].sd_cnt
= 0;
88 mtk_cooler_shutdown_dprintk("%s %s = %d\n", __func__
, tmp
, mtk_cl_sd_rst
);
93 int _mtk_cl_sd_rst_read(struct seq_file
*m
, void *v
)
98 static int _mtk_cl_sd_rst_open(struct inode
*inode
, struct file
*file
)
100 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
101 return single_open(file
, _mtk_cl_sd_rst_read
, PDE_DATA(inode
));
103 return single_open(file
, _mtk_cl_sd_rst_read
, PDE(inode
)->data
);
107 static const struct file_operations _cl_sd_rst_fops
= {
108 .owner
= THIS_MODULE
,
109 .open
= _mtk_cl_sd_rst_open
,
112 .write
= _mtk_cl_sd_rst_write
,
113 .release
= single_release
,
116 static ssize_t
_mtk_cl_sd_pid_write(struct file
*filp
, const char __user
*buf
, size_t len
,
120 char tmp
[MAX_LEN
] = { 0 };
122 len
= (len
< (MAX_LEN
-1)) ? len
: (MAX_LEN
-1);
123 /* write data to the buffer */
124 if (copy_from_user(tmp
, buf
, len
)) {
128 ret
= kstrtouint(tmp
, 10, &tm_input_pid
);
132 mtk_cooler_shutdown_dprintk("%s %s = %d\n", __func__
, tmp
, tm_input_pid
);
137 static int _mtk_cl_sd_pid_read(struct seq_file
*m
, void *v
)
139 seq_printf(m
, "%d\n", tm_input_pid
);
140 mtk_cooler_shutdown_dprintk("%s %d\n", __func__
, tm_input_pid
);
145 static int _mtk_cl_sd_pid_open(struct inode
*inode
, struct file
*file
)
147 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
148 return single_open(file
, _mtk_cl_sd_pid_read
, PDE_DATA(inode
));
150 return single_open(file
, _mtk_cl_sd_pid_read
, PDE(inode
)->data
);
154 static const struct file_operations _cl_sd_pid_fops
= {
155 .owner
= THIS_MODULE
,
156 .open
= _mtk_cl_sd_pid_open
,
159 .write
= _mtk_cl_sd_pid_write
,
160 .release
= single_release
,
163 static ssize_t
_mtk_cl_sd_debouncet_write(struct file
*filp
, const char __user
*buf
, size_t len
, loff_t
*data
)
165 char desc
[MAX_LEN
] = {0};
168 len
= (len
< (MAX_LEN
-1)) ? len
: (MAX_LEN
-1);
169 /* write data to the buffer */
170 if (copy_from_user(desc
, buf
, len
)) {
174 if(sscanf(desc
, "%d", &tmp_dbt
) == 1)
176 if (tmp_dbt
>= 0 && tmp_dbt
<= 5)
177 sd_debouncet
= tmp_dbt
;
179 mtk_cooler_shutdown_dprintk("[%s] oo range %s = %d\n", __func__
, desc
, sd_debouncet
);
182 mtk_cooler_shutdown_dprintk("[%s] bad arg %s = %d\n", __func__
, desc
, sd_debouncet
);
184 mtk_cooler_shutdown_dprintk("[%s] %s = %d\n", __func__
, desc
, sd_debouncet
);
189 static int _mtk_cl_sd_debouncet_read(struct seq_file
*m
, void *v
)
191 seq_printf(m
, "%d\n", sd_debouncet
);
192 mtk_cooler_shutdown_dprintk("[%s] %d\n", __func__
, sd_debouncet
);
197 static int _mtk_cl_sd_debouncet_open(struct inode
*inode
, struct file
*file
)
199 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
200 return single_open(file
, _mtk_cl_sd_debouncet_read
, PDE_DATA(inode
));
202 return single_open(file
, _mtk_cl_sd_debouncet_read
, PDE(inode
)->data
);
206 static const struct file_operations _cl_sd_debouncet_fops
= {
207 .owner
= THIS_MODULE
,
208 .open
= _mtk_cl_sd_debouncet_open
,
211 .write
= _mtk_cl_sd_debouncet_write
,
212 .release
= single_release
,
215 static int _mtk_cl_sd_send_signal(void)
219 if (tm_input_pid
== 0) {
220 mtk_cooler_shutdown_dprintk("%s pid is empty\n", __func__
);
224 mtk_cooler_shutdown_dprintk("%s pid is %d, %d\n", __func__
, tm_pid
, tm_input_pid
);
226 if (ret
== 0 && tm_input_pid
!= tm_pid
) {
227 tm_pid
= tm_input_pid
;
228 pg_task
= get_pid_task(find_vpid(tm_pid
), PIDTYPE_PID
);
231 if (ret
== 0 && pg_task
) {
233 info
.si_signo
= SIGIO
;
237 ret
= send_sig_info(SIGIO
, &info
, pg_task
);
241 mtk_cooler_shutdown_dprintk("%s ret=%d\n", __func__
, ret
);
248 static int mtk_cl_shutdown_get_max_state(struct thermal_cooling_device
*cdev
, unsigned long *state
)
251 /* mtk_cooler_shutdown_dprintk("mtk_cl_shutdown_get_max_state() %s %d\n", cdev->type, *state); */
255 static int mtk_cl_shutdown_get_cur_state(struct thermal_cooling_device
*cdev
, unsigned long *state
)
257 //*state = *((unsigned long *)cdev->devdata);
258 sd_state
*cl_state
= (sd_state
*) cdev
->devdata
;
260 *state
= cl_state
->state
;
261 /* mtk_cooler_shutdown_dprintk("mtk_cl_shutdown_get_cur_state() %s %d\n", cdev->type, *state); */
265 static int mtk_cl_shutdown_set_cur_state(struct thermal_cooling_device
*cdev
, unsigned long state
)
267 sd_state
*cl_state
= (sd_state
*) cdev
->devdata
;
268 #if defined(MTK_COOLER_SHUTDOWN_SIGNAL)
269 volatile unsigned long original_state
;
271 /* mtk_cooler_shutdown_dprintk("mtk_cl_shutdown_set_cur_state() %s %d\n", cdev->type, state); */
275 #if defined(MTK_COOLER_SHUTDOWN_SIGNAL)
276 original_state
= cl_state
->state
;
279 cl_state
->state
= state
;
283 if (cl_state
->sd_cnt
> 0)
293 if (sd_debouncet
== cl_state
->sd_cnt
) {
294 #if defined(MTK_COOLER_SHUTDOWN_UEVENT)
296 /* send uevent to notify current call must be dropped */
297 char event
[] = "SHUTDOWN=1";
298 char *envp
[] = { event
, NULL
};
300 kobject_uevent_env(&(cdev
->device
.kobj
), KOBJ_CHANGE
, envp
);
304 #if defined(MTK_COOLER_SHUTDOWN_SIGNAL)
305 if (0 == sd_happened
) /* make this an edge trigger instead of level trigger */
307 /* send signal to target process */
308 _mtk_cl_sd_send_signal();
317 /* bind fan callbacks to fan device */
318 static struct thermal_cooling_device_ops mtk_cl_shutdown_ops
= {
319 .get_max_state
= mtk_cl_shutdown_get_max_state
,
320 .get_cur_state
= mtk_cl_shutdown_get_cur_state
,
321 .set_cur_state
= mtk_cl_shutdown_set_cur_state
,
324 static int mtk_cooler_shutdown_register_ltf(void)
327 mtk_cooler_shutdown_dprintk("register ltf\n");
329 for (i
= MAX_NUM_INSTANCE_MTK_COOLER_SHUTDOWN
; i
-- > 0;) {
330 char temp
[20] = { 0 };
331 sprintf(temp
, "mtk-cl-shutdown%02d", i
);
332 cl_shutdown_dev
[i
] = mtk_thermal_cooling_device_register(temp
,
335 &mtk_cl_shutdown_ops
);
341 static void mtk_cooler_shutdown_unregister_ltf(void)
344 mtk_cooler_shutdown_dprintk("unregister ltf\n");
346 for (i
= MAX_NUM_INSTANCE_MTK_COOLER_SHUTDOWN
; i
-- > 0;) {
347 if (cl_shutdown_dev
[i
]) {
348 mtk_thermal_cooling_device_unregister(cl_shutdown_dev
[i
]);
349 cl_shutdown_dev
[i
] = NULL
;
350 cl_sd_state
[i
].state
= 0;
351 cl_sd_state
[i
].sd_cnt
= 0;
357 static int __init
mtk_cooler_shutdown_init(void)
362 for (i
= MAX_NUM_INSTANCE_MTK_COOLER_SHUTDOWN
; i
-- > 0;) {
363 cl_shutdown_dev
[i
] = NULL
;
364 cl_sd_state
[i
].state
= 0;
365 cl_sd_state
[i
].sd_cnt
= 0;
368 mtk_cooler_shutdown_dprintk("init\n");
370 #if defined(MTK_COOLER_SHUTDOWN_SIGNAL)
372 struct proc_dir_entry
*entry
= NULL
;
373 struct proc_dir_entry
*dir_entry
= mtk_thermal_get_proc_drv_therm_dir_entry();
376 mtk_cooler_shutdown_dprintk("%s mkdir /proc/driver/thermal failed\n", __func__
);
381 proc_create("clsd_pid", S_IRUGO
| S_IWUSR
| S_IWGRP
, dir_entry
,
384 mtk_cooler_shutdown_dprintk("%s clsd_pid creation failed\n",
387 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
388 proc_set_user(entry
, 0, 1000);
395 proc_create("clsd_rst", S_IRUGO
| S_IWUSR
| S_IWGRP
, dir_entry
,
398 mtk_cooler_shutdown_dprintk("%s clsd_rst creation failed\n",
401 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
402 proc_set_user(entry
, 0, 1000);
409 proc_create("clsd_dbt", S_IRUGO
| S_IWUSR
| S_IWGRP
, dir_entry
,
410 &_cl_sd_debouncet_fops
);
412 mtk_cooler_shutdown_dprintk("%s clsd_dbt creation failed\n",
415 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
416 proc_set_user(entry
, 0, 1000);
424 err
= mtk_cooler_shutdown_register_ltf();
431 mtk_cooler_shutdown_unregister_ltf();
435 static void __exit
mtk_cooler_shutdown_exit(void)
437 mtk_cooler_shutdown_dprintk("exit\n");
438 mtk_cooler_shutdown_unregister_ltf();
440 module_init(mtk_cooler_shutdown_init
);
441 module_exit(mtk_cooler_shutdown_exit
);