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