GNU Linux-libre 4.19.295-gnu1
[releases.git] / drivers / media / platform / rcar-fcp.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * rcar-fcp.c  --  R-Car Frame Compression Processor Driver
4  *
5  * Copyright (C) 2016 Renesas Electronics Corporation
6  *
7  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8  */
9
10 #include <linux/device.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/mutex.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19
20 #include <media/rcar-fcp.h>
21
22 struct rcar_fcp_device {
23         struct list_head list;
24         struct device *dev;
25         struct device_dma_parameters dma_parms;
26 };
27
28 static LIST_HEAD(fcp_devices);
29 static DEFINE_MUTEX(fcp_lock);
30
31 /* -----------------------------------------------------------------------------
32  * Public API
33  */
34
35 /**
36  * rcar_fcp_get - Find and acquire a reference to an FCP instance
37  * @np: Device node of the FCP instance
38  *
39  * Search the list of registered FCP instances for the instance corresponding to
40  * the given device node.
41  *
42  * Return a pointer to the FCP instance, or an ERR_PTR if the instance can't be
43  * found.
44  */
45 struct rcar_fcp_device *rcar_fcp_get(const struct device_node *np)
46 {
47         struct rcar_fcp_device *fcp;
48
49         mutex_lock(&fcp_lock);
50
51         list_for_each_entry(fcp, &fcp_devices, list) {
52                 if (fcp->dev->of_node != np)
53                         continue;
54
55                 get_device(fcp->dev);
56                 goto done;
57         }
58
59         fcp = ERR_PTR(-EPROBE_DEFER);
60
61 done:
62         mutex_unlock(&fcp_lock);
63         return fcp;
64 }
65 EXPORT_SYMBOL_GPL(rcar_fcp_get);
66
67 /**
68  * rcar_fcp_put - Release a reference to an FCP instance
69  * @fcp: The FCP instance
70  *
71  * Release the FCP instance acquired by a call to rcar_fcp_get().
72  */
73 void rcar_fcp_put(struct rcar_fcp_device *fcp)
74 {
75         if (fcp)
76                 put_device(fcp->dev);
77 }
78 EXPORT_SYMBOL_GPL(rcar_fcp_put);
79
80 struct device *rcar_fcp_get_device(struct rcar_fcp_device *fcp)
81 {
82         return fcp->dev;
83 }
84 EXPORT_SYMBOL_GPL(rcar_fcp_get_device);
85
86 /**
87  * rcar_fcp_enable - Enable an FCP
88  * @fcp: The FCP instance
89  *
90  * Before any memory access through an FCP is performed by a module, the FCP
91  * must be enabled by a call to this function. The enable calls are reference
92  * counted, each successful call must be followed by one rcar_fcp_disable()
93  * call when no more memory transfer can occur through the FCP.
94  *
95  * Return 0 on success or a negative error code if an error occurs. The enable
96  * reference count isn't increased when this function returns an error.
97  */
98 int rcar_fcp_enable(struct rcar_fcp_device *fcp)
99 {
100         int ret;
101
102         if (!fcp)
103                 return 0;
104
105         ret = pm_runtime_get_sync(fcp->dev);
106         if (ret < 0) {
107                 pm_runtime_put_noidle(fcp->dev);
108                 return ret;
109         }
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         fcp->dev->dma_parms = &fcp->dma_parms;
144         dma_set_max_seg_size(fcp->dev, DMA_BIT_MASK(32));
145
146         pm_runtime_enable(&pdev->dev);
147
148         mutex_lock(&fcp_lock);
149         list_add_tail(&fcp->list, &fcp_devices);
150         mutex_unlock(&fcp_lock);
151
152         platform_set_drvdata(pdev, fcp);
153
154         return 0;
155 }
156
157 static int rcar_fcp_remove(struct platform_device *pdev)
158 {
159         struct rcar_fcp_device *fcp = platform_get_drvdata(pdev);
160
161         mutex_lock(&fcp_lock);
162         list_del(&fcp->list);
163         mutex_unlock(&fcp_lock);
164
165         pm_runtime_disable(&pdev->dev);
166
167         return 0;
168 }
169
170 static const struct of_device_id rcar_fcp_of_match[] = {
171         { .compatible = "renesas,fcpf" },
172         { .compatible = "renesas,fcpv" },
173         { },
174 };
175 MODULE_DEVICE_TABLE(of, rcar_fcp_of_match);
176
177 static struct platform_driver rcar_fcp_platform_driver = {
178         .probe          = rcar_fcp_probe,
179         .remove         = rcar_fcp_remove,
180         .driver         = {
181                 .name   = "rcar-fcp",
182                 .of_match_table = rcar_fcp_of_match,
183                 .suppress_bind_attrs = true,
184         },
185 };
186
187 module_platform_driver(rcar_fcp_platform_driver);
188
189 MODULE_ALIAS("rcar-fcp");
190 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
191 MODULE_DESCRIPTION("Renesas FCP Driver");
192 MODULE_LICENSE("GPL");