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