summaryrefstree
path: root/kernel/drivers/thermal/ss_thermal_log.c
blob: f961fe5ccd794dff88b61a8c449ecf804f918b61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// SPDX-License-Identifier: GPL-2.0
/*
 *  ss_thermal_log.c - SAMSUNG Thermal logging.
 *
 */

#include <linux/module.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/thermal.h>
#include <linux/proc_fs.h>
#include <linux/time.h>
#include <linux/sched/clock.h>

/* TODO: move to general 'sec_pm_log' driver?? */
#define PROC_PM_DIR		"sec_pm_log"
static struct proc_dir_entry *procfs_power_dir;

/* thermal log */
#define THERMAL_LOG_NAME	"thermal_log"
#define	THERM_BUF_SIZE	SZ_128K
#define MAX_STR_LEN	120

static bool init_done __read_mostly = false;
static char thermal_log_buf[THERM_BUF_SIZE];
static unsigned int buf_pos;
static unsigned int buf_full;

void ss_thermal_print(const char *fmt, ...)
{
	char buf[MAX_STR_LEN] = {0, };
	unsigned int len = 0;
	va_list args;
	u64 time;
	unsigned long nsec;

	if (!init_done)
		return;

	/* timestamp */
	time = local_clock();
	nsec = do_div(time, NSEC_PER_SEC);
	len = snprintf(buf, sizeof(buf), "[%6lu.%06ld] ",
			(unsigned long)time, nsec / NSEC_PER_USEC);

	/* append log */
	va_start(args, fmt);
	len += vsnprintf(buf + len, MAX_STR_LEN - len, fmt, args);
	va_end(args);

	/* buffer full, reset position */
	if (buf_pos + len + 1 > THERM_BUF_SIZE) {
		buf_pos = 0;
		buf_full++;
	}

	buf_pos += scnprintf(&thermal_log_buf[buf_pos], len + 1, "%s", buf);
}

static ssize_t ss_thermal_log_read(struct file *file, char __user *buf,
						size_t len, loff_t *offset)
{
	loff_t pos = *offset;
	ssize_t count = 0;
	size_t size = (buf_full > 0) ? THERM_BUF_SIZE : (size_t)buf_pos;

	pr_info("%s: pos(%d), full(%d), size(%d)\n", __func__,
					buf_pos, buf_full, size);

	if (pos >= size)
		return 0;

	count = min(len, size);

	if ((pos + count) > size)
		count = size - pos;

	if (copy_to_user(buf, thermal_log_buf + pos, count))
		return -EFAULT;

	*offset += count;

	pr_info("%s: done\n", __func__);

	return count;
}


static const struct file_operations proc_ss_thermal_log_ops = {
	.owner 		= THIS_MODULE,
	.read		= ss_thermal_log_read,
};

void ss_thermal_log_init(void)
{
	struct proc_dir_entry *entry;
	pr_info("%s\n", __func__);

	procfs_power_dir = proc_mkdir(PROC_PM_DIR, NULL);
	if (unlikely(!procfs_power_dir))
		return;

	entry = proc_create(THERMAL_LOG_NAME, 0444,
				procfs_power_dir,
				&proc_ss_thermal_log_ops);
	if (unlikely(!entry))
		return;

	proc_set_size(entry, THERM_BUF_SIZE);

	init_done = true;

	ss_thermal_print("%s done\n", __func__);

	pr_info("%s: done\n", __func__);
}