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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/*
* Copyright (c) 2011, 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/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/mfd/pm8xxx/core.h>
#include <linux/debugfs.h>
#define PM8XXX_DEBUG_DEV_NAME "pm8xxx-debug"
struct pm8xxx_debug_device {
struct mutex debug_mutex;
struct device *parent;
struct dentry *dir;
int addr;
};
static bool pm8xxx_debug_addr_is_valid(int addr)
{
if (addr < 0 || addr > 0x3FF) {
pr_err("PMIC register address is invalid: %d\n", addr);
return false;
}
return true;
}
static int pm8xxx_debug_data_set(void *data, u64 val)
{
struct pm8xxx_debug_device *debugdev = data;
u8 reg = val;
int rc;
mutex_lock(&debugdev->debug_mutex);
if (pm8xxx_debug_addr_is_valid(debugdev->addr)) {
rc = pm8xxx_writeb(debugdev->parent, debugdev->addr, reg);
if (rc)
pr_err("pm8xxx_writeb(0x%03X)=0x%02X failed: rc=%d\n",
debugdev->addr, reg, rc);
}
mutex_unlock(&debugdev->debug_mutex);
return 0;
}
static int pm8xxx_debug_data_get(void *data, u64 *val)
{
struct pm8xxx_debug_device *debugdev = data;
int rc;
u8 reg;
mutex_lock(&debugdev->debug_mutex);
if (pm8xxx_debug_addr_is_valid(debugdev->addr)) {
rc = pm8xxx_readb(debugdev->parent, debugdev->addr, ®);
if (rc)
pr_err("pm8xxx_readb(0x%03X) failed: rc=%d\n",
debugdev->addr, rc);
else
*val = reg;
}
mutex_unlock(&debugdev->debug_mutex);
return 0;
}
DEFINE_SIMPLE_ATTRIBUTE(debug_data_fops, pm8xxx_debug_data_get,
pm8xxx_debug_data_set, "0x%02llX\n");
static int pm8xxx_debug_addr_set(void *data, u64 val)
{
struct pm8xxx_debug_device *debugdev = data;
if (pm8xxx_debug_addr_is_valid(val)) {
mutex_lock(&debugdev->debug_mutex);
debugdev->addr = val;
mutex_unlock(&debugdev->debug_mutex);
}
return 0;
}
static int pm8xxx_debug_addr_get(void *data, u64 *val)
{
struct pm8xxx_debug_device *debugdev = data;
mutex_lock(&debugdev->debug_mutex);
if (pm8xxx_debug_addr_is_valid(debugdev->addr))
*val = debugdev->addr;
mutex_unlock(&debugdev->debug_mutex);
return 0;
}
DEFINE_SIMPLE_ATTRIBUTE(debug_addr_fops, pm8xxx_debug_addr_get,
pm8xxx_debug_addr_set, "0x%03llX\n");
static int __devinit pm8xxx_debug_probe(struct platform_device *pdev)
{
char *name = pdev->dev.platform_data;
struct pm8xxx_debug_device *debugdev;
struct dentry *dir;
struct dentry *temp;
int rc;
if (name == NULL) {
pr_err("debugfs directory name must be specified in "
"platform_data pointer\n");
return -EINVAL;
}
debugdev = kzalloc(sizeof(struct pm8xxx_debug_device), GFP_KERNEL);
if (debugdev == NULL) {
pr_err("kzalloc failed\n");
return -ENOMEM;
}
debugdev->parent = pdev->dev.parent;
debugdev->addr = -1;
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("addr", S_IRUSR | 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_IRUSR | 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;
}
mutex_init(&debugdev->debug_mutex);
debugdev->dir = dir;
platform_set_drvdata(pdev, debugdev);
return 0;
file_error:
debugfs_remove_recursive(dir);
dir_error:
kfree(debugdev);
return rc;
}
static int __devexit pm8xxx_debug_remove(struct platform_device *pdev)
{
struct pm8xxx_debug_device *debugdev = platform_get_drvdata(pdev);
if (debugdev) {
debugfs_remove_recursive(debugdev->dir);
mutex_destroy(&debugdev->debug_mutex);
kfree(debugdev);
}
platform_set_drvdata(pdev, NULL);
return 0;
}
static struct platform_driver pm8xxx_debug_driver = {
.probe = pm8xxx_debug_probe,
.remove = __devexit_p(pm8xxx_debug_remove),
.driver = {
.name = PM8XXX_DEBUG_DEV_NAME,
.owner = THIS_MODULE,
},
};
static int __init pm8xxx_debug_init(void)
{
return platform_driver_register(&pm8xxx_debug_driver);
}
subsys_initcall(pm8xxx_debug_init);
static void __exit pm8xxx_debug_exit(void)
{
platform_driver_unregister(&pm8xxx_debug_driver);
}
module_exit(pm8xxx_debug_exit);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("PM8XXX Debug driver");
MODULE_VERSION("1.0");
MODULE_ALIAS("platform:" PM8XXX_DEBUG_DEV_NAME);
|