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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/* Copyright (c) 2010,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.
*
*/
/*
* Qualcomm PM8XXX UPL driver
*
*/
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/debugfs.h>
#include <linux/slab.h>
#include <linux/mfd/pm8xxx/core.h>
#include <linux/mfd/pm8xxx/upl.h>
/* PMIC8XXX UPL registers */
#define SSBI_REG_UPL_CTRL 0x17B
#define SSBI_REG_UPL_TRUTHTABLE1 0x17C
#define SSBI_REG_UPL_TRUTHTABLE2 0x17D
struct pm8xxx_upl_device {
struct device *dev;
struct mutex upl_mutex;
#if defined(CONFIG_DEBUG_FS)
struct dentry *dent;
#endif
};
static struct pm8xxx_upl_device *upl_dev;
/* APIs */
/*
* pm8xxx_upl_request - request a handle to access UPL device
*/
struct pm8xxx_upl_device *pm8xxx_upl_request(void)
{
return upl_dev;
}
EXPORT_SYMBOL(pm8xxx_upl_request);
/*
* pm8xxx_upl_read_truthtable - read value currently stored in UPL truth table
*
* @upldev: the UPL device
* @truthtable: value read from UPL truth table
*/
int pm8xxx_upl_read_truthtable(struct pm8xxx_upl_device *upldev,
u16 *truthtable)
{
int rc = 0;
u8 table[2];
if (upldev == NULL || IS_ERR(upldev))
return -EINVAL;
mutex_lock(&upldev->upl_mutex);
rc = pm8xxx_readb(upldev->dev->parent, SSBI_REG_UPL_TRUTHTABLE1,
&(table[0]));
if (rc) {
pr_err("%s: FAIL pm8xxx_readb(0x%X)=0x%02X: rc=%d\n",
__func__, SSBI_REG_UPL_TRUTHTABLE1, table[0], rc);
goto upl_read_done;
}
rc = pm8xxx_readb(upldev->dev->parent, SSBI_REG_UPL_TRUTHTABLE2,
&(table[1]));
if (rc)
pr_err("%s: FAIL pm8xxx_readb(0x%X)=0x%02X: rc=%d\n",
__func__, SSBI_REG_UPL_TRUTHTABLE2, table[1], rc);
upl_read_done:
mutex_unlock(&upldev->upl_mutex);
*truthtable = (((u16)table[1]) << 8) | table[0];
return rc;
}
EXPORT_SYMBOL(pm8xxx_upl_read_truthtable);
/*
* pm8xxx_upl_writes_truthtable - write value into UPL truth table
*
* @upldev: the UPL device
* @truthtable: value written to UPL truth table
*
* Each bit in parameter "truthtable" corresponds to the UPL output for a given
* set of input pin values. For example, if the input pins have the following
* values: A=1, B=1, C=1, D=0, then the UPL would output the value of bit 14
* (0b1110) in parameter "truthtable".
*/
int pm8xxx_upl_write_truthtable(struct pm8xxx_upl_device *upldev,
u16 truthtable)
{
int rc = 0;
u8 table[2];
if (upldev == NULL || IS_ERR(upldev))
return -EINVAL;
table[0] = truthtable & 0xFF;
table[1] = (truthtable >> 8) & 0xFF;
mutex_lock(&upldev->upl_mutex);
rc = pm8xxx_writeb(upldev->dev->parent, SSBI_REG_UPL_TRUTHTABLE1,
table[0]);
if (rc) {
pr_err("%s: FAIL pm8xxx_writeb(0x%X)=0x%04X: rc=%d\n",
__func__, SSBI_REG_UPL_TRUTHTABLE1, table[0], rc);
goto upl_write_done;
}
rc = pm8xxx_writeb(upldev->dev->parent, SSBI_REG_UPL_TRUTHTABLE2,
table[1]);
if (rc)
pr_err("%s: FAIL pm8xxx_writeb(0x%X)=0x%04X: rc=%d\n",
__func__, SSBI_REG_UPL_TRUTHTABLE2, table[1], rc);
upl_write_done:
mutex_unlock(&upldev->upl_mutex);
return rc;
}
EXPORT_SYMBOL(pm8xxx_upl_write_truthtable);
/*
* pm8xxx_upl_config - configure UPL I/O settings and UPL enable/disable
*
* @upldev: the UPL device
* @mask: setting mask to configure
* @flags: setting flags
*/
int pm8xxx_upl_config(struct pm8xxx_upl_device *upldev, u32 mask, u32 flags)
{
int rc;
u8 upl_ctrl, m, f;
if (upldev == NULL || IS_ERR(upldev))
return -EINVAL;
mutex_lock(&upldev->upl_mutex);
rc = pm8xxx_readb(upldev->dev->parent, SSBI_REG_UPL_CTRL, &upl_ctrl);
if (rc) {
pr_err("%s: FAIL pm8xxx_readb(0x%X)=0x%02X: rc=%d\n",
__func__, SSBI_REG_UPL_CTRL, upl_ctrl, rc);
goto upl_config_done;
}
m = mask & 0x00ff;
f = flags & 0x00ff;
upl_ctrl &= ~m;
upl_ctrl |= m & f;
rc = pm8xxx_writeb(upldev->dev->parent, SSBI_REG_UPL_CTRL, upl_ctrl);
if (rc)
pr_err("%s: FAIL pm8xxx_writeb(0x%X)=0x%02X: rc=%d\n",
__func__, SSBI_REG_UPL_CTRL, upl_ctrl, rc);
upl_config_done:
mutex_unlock(&upldev->upl_mutex);
return rc;
}
EXPORT_SYMBOL(pm8xxx_upl_config);
#if defined(CONFIG_DEBUG_FS)
static int truthtable_set(void *data, u64 val)
{
int rc;
rc = pm8xxx_upl_write_truthtable(data, val);
if (rc)
pr_err("%s: pm8xxx_upl_write_truthtable: rc=%d, "
"truthtable=0x%llX\n", __func__, rc, val);
return rc;
}
static int truthtable_get(void *data, u64 *val)
{
int rc;
u16 truthtable;
rc = pm8xxx_upl_read_truthtable(data, &truthtable);
if (rc)
pr_err("%s: pm8xxx_upl_read_truthtable: rc=%d, "
"truthtable=0x%X\n", __func__, rc, truthtable);
if (val)
*val = truthtable;
return rc;
}
DEFINE_SIMPLE_ATTRIBUTE(upl_truthtable_fops, truthtable_get,
truthtable_set, "0x%04llX\n");
/* enter values as 0xMMMMFFFF where MMMM is the mask and FFFF is the flags */
static int control_set(void *data, u64 val)
{
u8 mask, flags;
int rc;
flags = val & 0xFFFF;
mask = (val >> 16) & 0xFFFF;
rc = pm8xxx_upl_config(data, mask, flags);
if (rc)
pr_err("%s: pm8xxx_upl_config: rc=%d, mask = 0x%X, "
"flags = 0x%X\n", __func__, rc, mask, flags);
return rc;
}
static int control_get(void *data, u64 *val)
{
struct pm8xxx_upl_device *upldev;
int rc = 0;
u8 ctrl;
upldev = data;
mutex_lock(&upldev->upl_mutex);
rc = pm8xxx_readb(upldev->dev->parent, SSBI_REG_UPL_CTRL, &ctrl);
if (rc)
pr_err("%s: FAIL pm8xxx_readb(): rc=%d (ctrl=0x%02X)\n",
__func__, rc, ctrl);
mutex_unlock(&upldev->upl_mutex);
*val = ctrl;
return rc;
}
DEFINE_SIMPLE_ATTRIBUTE(upl_control_fops, control_get,
control_set, "0x%02llX\n");
static int pm8xxx_upl_debug_init(struct pm8xxx_upl_device *upldev)
{
struct dentry *dent;
struct dentry *temp;
dent = debugfs_create_dir("pm8xxx-upl", NULL);
if (dent == NULL || IS_ERR(dent)) {
pr_err("%s: ERR debugfs_create_dir: dent=0x%X\n",
__func__, (unsigned)dent);
return -ENOMEM;
}
temp = debugfs_create_file("truthtable", S_IRUSR | S_IWUSR, dent,
upldev, &upl_truthtable_fops);
if (temp == NULL || IS_ERR(temp)) {
pr_err("%s: ERR debugfs_create_file: dent=0x%X\n",
__func__, (unsigned)dent);
goto debug_error;
}
temp = debugfs_create_file("control", S_IRUSR | S_IWUSR, dent,
upldev, &upl_control_fops);
if (temp == NULL || IS_ERR(temp)) {
pr_err("%s: ERR debugfs_create_file: dent=0x%X\n",
__func__, (unsigned)dent);
goto debug_error;
}
upldev->dent = dent;
return 0;
debug_error:
debugfs_remove_recursive(dent);
return -ENOMEM;
}
static int __devexit pm8xxx_upl_debug_remove(struct pm8xxx_upl_device *upldev)
{
debugfs_remove_recursive(upldev->dent);
return 0;
}
#endif /* CONFIG_DEBUG_FS */
static int __devinit pm8xxx_upl_probe(struct platform_device *pdev)
{
struct pm8xxx_upl_device *upldev;
upldev = kzalloc(sizeof *upldev, GFP_KERNEL);
if (upldev == NULL) {
pr_err("%s: kzalloc() failed.\n", __func__);
return -ENOMEM;
}
mutex_init(&upldev->upl_mutex);
upl_dev = upldev;
upldev->dev = &pdev->dev;
platform_set_drvdata(pdev, upldev);
#if defined(CONFIG_DEBUG_FS)
pm8xxx_upl_debug_init(upl_dev);
#endif
pr_notice("%s: OK\n", __func__);
return 0;
}
static int __devexit pm8xxx_upl_remove(struct platform_device *pdev)
{
struct pm8xxx_upl_device *upldev = platform_get_drvdata(pdev);
#if defined(CONFIG_DEBUG_FS)
pm8xxx_upl_debug_remove(upldev);
#endif
platform_set_drvdata(pdev, NULL);
kfree(upldev);
pr_notice("%s: OK\n", __func__);
return 0;
}
static struct platform_driver pm8xxx_upl_driver = {
.probe = pm8xxx_upl_probe,
.remove = __devexit_p(pm8xxx_upl_remove),
.driver = {
.name = PM8XXX_UPL_DEV_NAME,
.owner = THIS_MODULE,
},
};
static int __init pm8xxx_upl_init(void)
{
return platform_driver_register(&pm8xxx_upl_driver);
}
static void __exit pm8xxx_upl_exit(void)
{
platform_driver_unregister(&pm8xxx_upl_driver);
}
module_init(pm8xxx_upl_init);
module_exit(pm8xxx_upl_exit);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("PM8XXX UPL driver");
MODULE_VERSION("1.0");
MODULE_ALIAS("platform:" PM8XXX_UPL_DEV_NAME);
|