aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/remoteproc/rpres.c
blob: 522b1ce3ed930f42e2be1809d6081b0ec301bbb0 (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
/*
 * Remote processor resources
 *
 * Copyright (C) 2011 Texas Instruments, Inc.
 * Copyright (C) 2011 Google, Inc.
 *
 * Fernando Guzman Lugo <fernando.lugo@ti.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <plat/omap_device.h>
#include <plat/rpres.h>

static LIST_HEAD(rpres_list);
static DEFINE_SPINLOCK(rpres_lock);

static struct rpres *__find_by_name(const char *name)
{
	struct rpres *obj;

	list_for_each_entry(obj, &rpres_list, next)
		if (!strcmp(obj->name, name))
			return obj;
	return NULL;
}

struct rpres *rpres_get(const char *name)
{
	int ret;
	struct rpres *r;
	struct rpres_platform_data *pdata;

	spin_lock(&rpres_lock);
	r = __find_by_name(name);
	spin_unlock(&rpres_lock);
	if (!r)
		return ERR_PTR(-ENOENT);

	mutex_lock(&r->lock);
	if (r->state == RPRES_ACTIVE) {
		pr_err("%s:resource already active\n", __func__);
		ret = -EINVAL;
		goto out;
	}
	pdata = r->pdev->dev.platform_data;
	ret = pdata->ops->start(r->pdev);
	if (!ret)
		r->state = RPRES_ACTIVE;
out:
	mutex_unlock(&r->lock);
	if (ret)
		return ERR_PTR(ret);
	return r;
}
EXPORT_SYMBOL(rpres_get);

void rpres_put(struct rpres *obj)
{
	struct rpres_platform_data *pdata = obj->pdev->dev.platform_data;
	mutex_lock(&obj->lock);
	if (obj->state == RPRES_INACTIVE) {
		pr_err("%s:resource already inactive\n", __func__);
	} else {
		pdata->ops->stop(obj->pdev);
		obj->state = RPRES_INACTIVE;
	}
	mutex_unlock(&obj->lock);
}
EXPORT_SYMBOL(rpres_put);

static int rpres_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct rpres_platform_data *pdata = dev->platform_data;
	struct rpres *obj;

	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
	if (!obj)
		return -ENOMEM;

	obj->pdev = pdev;
	obj->name = pdata->name;
	obj->state = RPRES_INACTIVE;
	mutex_init(&obj->lock);

	spin_lock(&rpres_lock);
	list_add_tail(&obj->next, &rpres_list);
	spin_unlock(&rpres_lock);

	return 0;
}

static int __devexit rpres_remove(struct platform_device *pdev)
{
	struct rpres_platform_data *pdata = pdev->dev.platform_data;
	struct rpres *obj;

	spin_lock(&rpres_lock);
	obj = __find_by_name(pdata->name);
	if (!obj) {
		spin_unlock(&rpres_lock);
		dev_err(&pdev->dev, "fail to remove %s\n", pdata->name);
		return -ENOENT;
	}
	list_del(&obj->next);
	spin_unlock(&rpres_lock);

	kfree(obj);

	return 0;
}

static struct platform_driver omap_rpres_driver = {
	.probe = rpres_probe,
	.remove = __devexit_p(rpres_remove),
	.driver = {
		.name = "rpres",
		.owner = THIS_MODULE,
	},
};

static int __init rpres_init(void)
{
	return platform_driver_register(&omap_rpres_driver);
}
module_init(rpres_init);

static void __exit rpres_exit(void)
{
	platform_driver_unregister(&omap_rpres_driver);
}
module_exit(rpres_exit);

MODULE_LICENSE("GPL v2");