1 // SPDX-License-Identifier: GPL-2.0+
3 * rcar-fcp.c -- R-Car Frame Compression Processor Driver
5 * Copyright (C) 2016 Renesas Electronics Corporation
7 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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>
19 #include <media/rcar-fcp.h>
21 struct rcar_fcp_device
{
22 struct list_head list
;
26 static LIST_HEAD(fcp_devices
);
27 static DEFINE_MUTEX(fcp_lock
);
29 /* -----------------------------------------------------------------------------
34 * rcar_fcp_get - Find and acquire a reference to an FCP instance
35 * @np: Device node of the FCP instance
37 * Search the list of registered FCP instances for the instance corresponding to
38 * the given device node.
40 * Return a pointer to the FCP instance, or an ERR_PTR if the instance can't be
43 struct rcar_fcp_device
*rcar_fcp_get(const struct device_node
*np
)
45 struct rcar_fcp_device
*fcp
;
47 mutex_lock(&fcp_lock
);
49 list_for_each_entry(fcp
, &fcp_devices
, list
) {
50 if (fcp
->dev
->of_node
!= np
)
57 fcp
= ERR_PTR(-EPROBE_DEFER
);
60 mutex_unlock(&fcp_lock
);
63 EXPORT_SYMBOL_GPL(rcar_fcp_get
);
66 * rcar_fcp_put - Release a reference to an FCP instance
67 * @fcp: The FCP instance
69 * Release the FCP instance acquired by a call to rcar_fcp_get().
71 void rcar_fcp_put(struct rcar_fcp_device
*fcp
)
76 EXPORT_SYMBOL_GPL(rcar_fcp_put
);
78 struct device
*rcar_fcp_get_device(struct rcar_fcp_device
*fcp
)
82 EXPORT_SYMBOL_GPL(rcar_fcp_get_device
);
85 * rcar_fcp_enable - Enable an FCP
86 * @fcp: The FCP instance
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.
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.
96 int rcar_fcp_enable(struct rcar_fcp_device
*fcp
)
103 ret
= pm_runtime_get_sync(fcp
->dev
);
109 EXPORT_SYMBOL_GPL(rcar_fcp_enable
);
112 * rcar_fcp_disable - Disable an FCP
113 * @fcp: The FCP instance
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.
118 void rcar_fcp_disable(struct rcar_fcp_device
*fcp
)
121 pm_runtime_put(fcp
->dev
);
123 EXPORT_SYMBOL_GPL(rcar_fcp_disable
);
125 /* -----------------------------------------------------------------------------
129 static int rcar_fcp_probe(struct platform_device
*pdev
)
131 struct rcar_fcp_device
*fcp
;
133 fcp
= devm_kzalloc(&pdev
->dev
, sizeof(*fcp
), GFP_KERNEL
);
137 fcp
->dev
= &pdev
->dev
;
139 pm_runtime_enable(&pdev
->dev
);
141 mutex_lock(&fcp_lock
);
142 list_add_tail(&fcp
->list
, &fcp_devices
);
143 mutex_unlock(&fcp_lock
);
145 platform_set_drvdata(pdev
, fcp
);
150 static int rcar_fcp_remove(struct platform_device
*pdev
)
152 struct rcar_fcp_device
*fcp
= platform_get_drvdata(pdev
);
154 mutex_lock(&fcp_lock
);
155 list_del(&fcp
->list
);
156 mutex_unlock(&fcp_lock
);
158 pm_runtime_disable(&pdev
->dev
);
163 static const struct of_device_id rcar_fcp_of_match
[] = {
164 { .compatible
= "renesas,fcpf" },
165 { .compatible
= "renesas,fcpv" },
168 MODULE_DEVICE_TABLE(of
, rcar_fcp_of_match
);
170 static struct platform_driver rcar_fcp_platform_driver
= {
171 .probe
= rcar_fcp_probe
,
172 .remove
= rcar_fcp_remove
,
175 .of_match_table
= rcar_fcp_of_match
,
176 .suppress_bind_attrs
= true,
180 module_platform_driver(rcar_fcp_platform_driver
);
182 MODULE_ALIAS("rcar-fcp");
183 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
184 MODULE_DESCRIPTION("Renesas FCP Driver");
185 MODULE_LICENSE("GPL");