Merge remote-tracking branches 'asoc/topic/rt5659', 'asoc/topic/rt5660', 'asoc/topic...
[cascardo/linux.git] / drivers / media / platform / rcar-fcp.c
1 /*
2  * rcar-fcp.c  --  R-Car Frame Compression Processor Driver
3  *
4  * Copyright (C) 2016 Renesas Electronics Corporation
5  *
6  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <linux/device.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/slab.h>
21
22 #include <media/rcar-fcp.h>
23
24 struct rcar_fcp_device {
25         struct list_head list;
26         struct device *dev;
27 };
28
29 static LIST_HEAD(fcp_devices);
30 static DEFINE_MUTEX(fcp_lock);
31
32 /* -----------------------------------------------------------------------------
33  * Public API
34  */
35
36 /**
37  * rcar_fcp_get - Find and acquire a reference to an FCP instance
38  * @np: Device node of the FCP instance
39  *
40  * Search the list of registered FCP instances for the instance corresponding to
41  * the given device node.
42  *
43  * Return a pointer to the FCP instance, or an ERR_PTR if the instance can't be
44  * found.
45  */
46 struct rcar_fcp_device *rcar_fcp_get(const struct device_node *np)
47 {
48         struct rcar_fcp_device *fcp;
49
50         mutex_lock(&fcp_lock);
51
52         list_for_each_entry(fcp, &fcp_devices, list) {
53                 if (fcp->dev->of_node != np)
54                         continue;
55
56                 /*
57                  * Make sure the module won't be unloaded behind our back. This
58                  * is a poor man's safety net, the module should really not be
59                  * unloaded while FCP users can be active.
60                  */
61                 if (!try_module_get(fcp->dev->driver->owner))
62                         fcp = NULL;
63
64                 goto done;
65         }
66
67         fcp = ERR_PTR(-EPROBE_DEFER);
68
69 done:
70         mutex_unlock(&fcp_lock);
71         return fcp;
72 }
73 EXPORT_SYMBOL_GPL(rcar_fcp_get);
74
75 /**
76  * rcar_fcp_put - Release a reference to an FCP instance
77  * @fcp: The FCP instance
78  *
79  * Release the FCP instance acquired by a call to rcar_fcp_get().
80  */
81 void rcar_fcp_put(struct rcar_fcp_device *fcp)
82 {
83         if (fcp)
84                 module_put(fcp->dev->driver->owner);
85 }
86 EXPORT_SYMBOL_GPL(rcar_fcp_put);
87
88 /**
89  * rcar_fcp_enable - Enable an FCP
90  * @fcp: The FCP instance
91  *
92  * Before any memory access through an FCP is performed by a module, the FCP
93  * must be enabled by a call to this function. The enable calls are reference
94  * counted, each successful call must be followed by one rcar_fcp_disable()
95  * call when no more memory transfer can occur through the FCP.
96  *
97  * Return 0 on success or a negative error code if an error occurs. The enable
98  * reference count isn't increased when this function returns an error.
99  */
100 int rcar_fcp_enable(struct rcar_fcp_device *fcp)
101 {
102         int error;
103
104         if (!fcp)
105                 return 0;
106
107         error = pm_runtime_get_sync(fcp->dev);
108         if (error < 0)
109                 return error;
110
111         return 0;
112 }
113 EXPORT_SYMBOL_GPL(rcar_fcp_enable);
114
115 /**
116  * rcar_fcp_disable - Disable an FCP
117  * @fcp: The FCP instance
118  *
119  * This function is the counterpart of rcar_fcp_enable(). As enable calls are
120  * reference counted a disable call may not disable the FCP synchronously.
121  */
122 void rcar_fcp_disable(struct rcar_fcp_device *fcp)
123 {
124         if (fcp)
125                 pm_runtime_put(fcp->dev);
126 }
127 EXPORT_SYMBOL_GPL(rcar_fcp_disable);
128
129 /* -----------------------------------------------------------------------------
130  * Platform Driver
131  */
132
133 static int rcar_fcp_probe(struct platform_device *pdev)
134 {
135         struct rcar_fcp_device *fcp;
136
137         fcp = devm_kzalloc(&pdev->dev, sizeof(*fcp), GFP_KERNEL);
138         if (fcp == NULL)
139                 return -ENOMEM;
140
141         fcp->dev = &pdev->dev;
142
143         pm_runtime_enable(&pdev->dev);
144
145         mutex_lock(&fcp_lock);
146         list_add_tail(&fcp->list, &fcp_devices);
147         mutex_unlock(&fcp_lock);
148
149         platform_set_drvdata(pdev, fcp);
150
151         return 0;
152 }
153
154 static int rcar_fcp_remove(struct platform_device *pdev)
155 {
156         struct rcar_fcp_device *fcp = platform_get_drvdata(pdev);
157
158         mutex_lock(&fcp_lock);
159         list_del(&fcp->list);
160         mutex_unlock(&fcp_lock);
161
162         pm_runtime_disable(&pdev->dev);
163
164         return 0;
165 }
166
167 static const struct of_device_id rcar_fcp_of_match[] = {
168         { .compatible = "renesas,fcpv" },
169         { },
170 };
171
172 static struct platform_driver rcar_fcp_platform_driver = {
173         .probe          = rcar_fcp_probe,
174         .remove         = rcar_fcp_remove,
175         .driver         = {
176                 .name   = "rcar-fcp",
177                 .of_match_table = rcar_fcp_of_match,
178                 .suppress_bind_attrs = true,
179         },
180 };
181
182 module_platform_driver(rcar_fcp_platform_driver);
183
184 MODULE_ALIAS("rcar-fcp");
185 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
186 MODULE_DESCRIPTION("Renesas FCP Driver");
187 MODULE_LICENSE("GPL");