-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.c
357 lines (283 loc) · 8.89 KB
/
device.c
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
351
352
353
354
355
356
357
/*
* File: vpd-device.c
*
* power test module
*
* Copyright (C) 2015 Anapass Inc.
* Author: Youngdo, Lee <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <linux/module.h>
#include <linux/device.h>
#include <linux/pm_runtime.h>
#include <linux/platform_device.h>
#include "domain.h"
#include "debug.h"
/*****************************************************************************/
#define VPD_MODULE_NAME "vpd"
#define ATTR_PTR(_name) (&dev_attr_##_name)
/*****************************************************************************/
struct vpd_context {
struct mutex lock; // refcnt lock
int refcnt;
};
/*****************************************************************************/
#ifdef CONFIG_PM_SLEEP
static int vpd_device_suspend(struct device *dev)
{
struct platform_device *pdev = to_platform_device(dev);
struct vpd_context *vpdc = dev_get_drvdata(dev);
mutex_lock(&vpdc->lock);
DBG("=====> [%s-%d] SYSTEM SUSPEND (refcnt = %d) <=====",
pdev->name, pdev->id, vpdc->refcnt);
mutex_unlock(&vpdc->lock);
return 0;
}
static int vpd_device_resume(struct device *dev)
{
struct platform_device *pdev = to_platform_device(dev);
struct vpd_context *vpdc = dev_get_drvdata(dev);
mutex_lock(&vpdc->lock);
DBG("=====> [%s-%d] SYSTEM RESUME(refcnt = %d) <=====",
pdev->name, pdev->id, vpdc->refcnt);
mutex_unlock(&vpdc->lock);
return 0;
}
#endif /*CONFIG_PM_SLEEP*/
#ifdef CONFIG_PM_RUNTIME
static int vpd_device_runtime_suspend(struct device *dev)
{
struct platform_device *pdev = to_platform_device(dev);
struct vpd_context *vpdc = dev_get_drvdata(dev);
mutex_lock(&vpdc->lock);
DBG("=====> [%s-%d] RUNTIME SUSPEND(refcnt = %d) <=====",
pdev->name, pdev->id, vpdc->refcnt);
mutex_unlock(&vpdc->lock);
return 0;
}
static int vpd_device_runtime_resume(struct device *dev)
{
struct platform_device *pdev = to_platform_device(dev);
struct vpd_context *vpdc = dev_get_drvdata(dev);
mutex_lock(&vpdc->lock);
DBG("=====> [%s-%d] RUNTIME RESUME(refcnt = %d) <=====",
pdev->name, pdev->id, vpdc->refcnt);
mutex_unlock(&vpdc->lock);
return 0;
}
#endif /*CONFIG_PM_RUNTIME*/
/*****************************************************************************/
static const struct dev_pm_ops vpd_device_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(
vpd_device_suspend, vpd_device_resume)
SET_RUNTIME_PM_OPS(
vpd_device_runtime_suspend, vpd_device_runtime_resume, NULL)
};
/*****************************************************************************/
static ssize_t vpd_device_sysfs_refcnt_show(
struct device *dev, struct device_attribute *attr, char *buf)
{
ssize_t ret;
struct vpd_context *vpdc = dev_get_drvdata(dev);
mutex_lock(&vpdc->lock);
ret = sprintf(buf, "%d\n", vpdc->refcnt);
mutex_unlock(&vpdc->lock);
return ret;
}
static ssize_t vpd_device_sysfs_open_store(
struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct vpd_context *vpdc = dev_get_drvdata(dev);
pm_runtime_get_sync(dev);
mutex_lock(&vpdc->lock);
vpdc->refcnt++;
mutex_unlock(&vpdc->lock);
DBG("refcnt = %d", vpdc->refcnt);
return count;
}
static ssize_t vpd_device_sysfs_close_store(
struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
bool unlock = false;
struct vpd_context *vpdc = dev_get_drvdata(dev);
mutex_lock(&vpdc->lock);
if (vpdc->refcnt > 0) {
unlock = true;
vpdc->refcnt--;
}
mutex_unlock(&vpdc->lock);
if (unlock)
pm_runtime_put(dev);
DBG("refcnt = %d", vpdc->refcnt);
return count;
}
static DEVICE_ATTR(refcnt, S_IRUGO, vpd_device_sysfs_refcnt_show, NULL);
static DEVICE_ATTR(open, S_IWUSR|S_IWGRP, NULL, vpd_device_sysfs_open_store);
static DEVICE_ATTR(close, S_IWUSR|S_IWGRP, NULL, vpd_device_sysfs_close_store);
static struct device_attribute *attrs[] = {
ATTR_PTR(refcnt),
ATTR_PTR(open),
ATTR_PTR(close),
};
static void vpd_device_init_sysfs(struct platform_device *pdev)
{
int i;
int ret;
TRACE();
for (i = 0; i < ARRAY_SIZE(attrs); i++) {
ret = device_create_file(&pdev->dev, attrs[i]);
if (ret) {
ERR("device_create_file failed.");
for (--i; i >= 0; i--)
device_remove_file(&pdev->dev, attrs[i]);
break;
}
}
}
static void vpd_device_exit_sysfs(struct platform_device *pdev)
{
int i;
TRACE();
for (i = 0; i < ARRAY_SIZE(attrs); i++)
device_remove_file(&pdev->dev, attrs[i]);
}
static int vpd_device_probe(struct platform_device *pdev)
{
int ret;
struct vpd_context *vpdc;
TRACE();
vpdc = devm_kzalloc(&pdev->dev, sizeof(*vpdc), GFP_KERNEL);
BUG_ON(!vpdc);
mutex_init(&vpdc->lock);
vpdc->refcnt = 0;
platform_set_drvdata(pdev, vpdc);
pm_runtime_enable(&pdev->dev);
ret = pm_runtime_get_sync(&pdev->dev);
if (ret) {
ERR("pm_runtime_get_sync failed.(%d)", ret);
return ret;
}
TRACE();
vpd_device_init_sysfs(pdev);
pm_runtime_put(&pdev->dev);
DBG(VPD_MODULE_NAME "-%d probed.", pdev->id);
return 0;
}
static int vpd_device_remove(struct platform_device *pdev)
{
struct vpd_context *vpdc;
TRACE();
pm_runtime_disable(&pdev->dev);
pm_runtime_set_suspended(&pdev->dev);
vpdc = platform_get_drvdata(pdev);
devm_kfree(&pdev->dev, vpdc);
vpd_device_exit_sysfs(pdev);
DBG(VPD_MODULE_NAME "-%d removed.", pdev->id);
return 0;
}
static void vpd_device_device_release(struct device *dev)
{
}
static struct platform_device_id vpd_device_ids[] = {
{
.name = VPD_MODULE_NAME,
},
{ },
};
static struct platform_driver vpd_platform_driver = {
.probe = vpd_device_probe,
.remove = vpd_device_remove,
.id_table = vpd_device_ids,
.driver = {
.name = VPD_MODULE_NAME,
.owner = THIS_MODULE,
.pm = &vpd_device_pm_ops,
},
};
static struct platform_device vpd_platform_devices[] = {
[0] = {
.name = VPD_MODULE_NAME,
.id = 0,
.num_resources = 0,
.resource = NULL,
.dev = {
.release = vpd_device_device_release,
.platform_data = NULL,
.pm_domain = &vpd_core_domain.pm_domain,
},
},
[1] = {
.name = VPD_MODULE_NAME,
.id = 1,
.num_resources = 0,
.resource = NULL,
.dev = {
.release = vpd_device_device_release,
.platform_data = NULL,
.pm_domain = &vpd_video_domain.pm_domain,
},
},
[2] = {
.name = VPD_MODULE_NAME,
.id = 2,
.num_resources = 0,
.resource = NULL,
.dev = {
.release = vpd_device_device_release,
.platform_data = NULL,
.pm_domain = &vpd_video_domain.pm_domain,
},
},
};
/*****************************************************************************/
static int __init vpd_init(void)
{
int i;
int ret;
ret = platform_driver_register(&vpd_platform_driver);
if (ret) {
ERR("vpd_device_platform_driver register failed.");
return ret;
}
INFO("vpd_device_platform_driver installed.");
for (i = 0; i < ARRAY_SIZE(vpd_platform_devices); i++) {
ret = platform_device_register(&vpd_platform_devices[i]);
if (ret) {
for (--i; i >=0; i--)
platform_device_unregister(&vpd_platform_devices[i]);
platform_driver_unregister(&vpd_platform_driver);
ERR("vpd_device_device register failed.");
return ret;
}
}
INFO("vpd_platform_device installed.");
return 0;
}
static void __exit vpd_exit(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(vpd_platform_devices); i++)
platform_device_unregister(&vpd_platform_devices[i]);
platform_driver_unregister(&vpd_platform_driver);
}
/*****************************************************************************/
module_init(vpd_init);
module_exit(vpd_exit);
MODULE_AUTHOR("Youngdo, Lee <[email protected]>");
MODULE_DESCRIPTION("Power Test Driver");
MODULE_LICENSE("GPL");