aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/mach-msm/msm_cpr-debug.c
blob: 3564bbeeb2e81fcb9030e60b13b6242a1740d817 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#define pr_fmt(fmt) "%s: " fmt, __func__

#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/debugfs.h>
#include <linux/module.h>

struct msm_cpr_debug_device {
	struct mutex	debug_mutex;
	struct dentry	*dir;
	int		addr_offset;
	void __iomem	*base;
};

static inline
void write_reg(struct msm_cpr_debug_device *cpr, u32 value)
{
	writel_relaxed(value, cpr->base + cpr->addr_offset);
}

static inline u32 read_reg(struct msm_cpr_debug_device *cpr)
{
	return readl_relaxed(cpr->base + cpr->addr_offset);
}

static bool msm_cpr_debug_addr_is_valid(int addr)
{
	if (addr < 0 || addr > 0x15C) {
		pr_err("CPR register address is invalid: %d\n", addr);
		return false;
	}
	return true;
}

static int msm_cpr_debug_data_set(void *data, u64 val)
{
	struct msm_cpr_debug_device *debugdev = data;
	uint32_t reg = val;

	mutex_lock(&debugdev->debug_mutex);

	if (msm_cpr_debug_addr_is_valid(debugdev->addr_offset))
		write_reg(debugdev, reg);

	mutex_unlock(&debugdev->debug_mutex);
	return 0;
}

static int msm_cpr_debug_data_get(void *data, u64 *val)
{
	struct msm_cpr_debug_device *debugdev = data;
	uint32_t reg;

	mutex_lock(&debugdev->debug_mutex);

	if (msm_cpr_debug_addr_is_valid(debugdev->addr_offset)) {
		reg = read_reg(debugdev);
		*val = reg;
	}
	mutex_unlock(&debugdev->debug_mutex);
	return 0;
}

DEFINE_SIMPLE_ATTRIBUTE(debug_data_fops, msm_cpr_debug_data_get,
			msm_cpr_debug_data_set, "0x%02llX\n");

static int msm_cpr_debug_addr_set(void *data, u64 val)
{
	struct msm_cpr_debug_device *debugdev = data;

	if (msm_cpr_debug_addr_is_valid(val)) {
		mutex_lock(&debugdev->debug_mutex);
		debugdev->addr_offset = val;
		mutex_unlock(&debugdev->debug_mutex);
	}

	return 0;
}

static int msm_cpr_debug_addr_get(void *data, u64 *val)
{
	struct msm_cpr_debug_device *debugdev = data;

	mutex_lock(&debugdev->debug_mutex);

	if (msm_cpr_debug_addr_is_valid(debugdev->addr_offset))
		*val = debugdev->addr_offset;

	mutex_unlock(&debugdev->debug_mutex);
	return 0;
}

DEFINE_SIMPLE_ATTRIBUTE(debug_addr_fops, msm_cpr_debug_addr_get,
			msm_cpr_debug_addr_set, "0x%03llX\n");

int msm_cpr_debug_init(void *data)
{
	char *name = "cpr-debug";
	struct msm_cpr_debug_device *debugdev;
	struct dentry *dir;
	struct dentry *temp;
	int rc;

	debugdev = kzalloc(sizeof(struct msm_cpr_debug_device), GFP_KERNEL);
	if (debugdev == NULL) {
		pr_err("kzalloc failed\n");
		return -ENOMEM;
	}

	dir = debugfs_create_dir(name, NULL);
	if (dir == NULL || IS_ERR(dir)) {
		pr_err("debugfs_create_dir failed: rc=%ld\n", PTR_ERR(dir));
		rc = PTR_ERR(dir);
		goto dir_error;
	}

	temp = debugfs_create_file("address", S_IRUGO | S_IWUSR, dir, debugdev,
				   &debug_addr_fops);
	if (temp == NULL || IS_ERR(temp)) {
		pr_err("debugfs_create_file failed: rc=%ld\n", PTR_ERR(temp));
		rc = PTR_ERR(temp);
		goto file_error;
	}

	temp = debugfs_create_file("data", S_IRUGO | S_IWUSR, dir, debugdev,
				   &debug_data_fops);
	if (temp == NULL  || IS_ERR(temp)) {
		pr_err("debugfs_create_file failed: rc=%ld\n", PTR_ERR(temp));
		rc = PTR_ERR(temp);
		goto file_error;
	}
	debugdev->base = data;
	debugdev->addr_offset = -1;
	debugdev->dir = dir;
	mutex_init(&debugdev->debug_mutex);

	return 0;

file_error:
	debugfs_remove_recursive(dir);
dir_error:
	kfree(debugdev);

	return rc;
}