1 // SPDX-License-Identifier: GPL-2.0+
3 * rcar-fcp.c -- R-Car Frame Compression Processor Driver
5 * Copyright (C) 2016 Renesas Electronics Corporation
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>
20 #include <media/rcar-fcp.h>
22 struct rcar_fcp_device {
23 struct list_head list;
27 static LIST_HEAD(fcp_devices);
28 static DEFINE_MUTEX(fcp_lock);
30 /* -----------------------------------------------------------------------------
35 * rcar_fcp_get - Find and acquire a reference to an FCP instance
36 * @np: Device node of the FCP instance
38 * Search the list of registered FCP instances for the instance corresponding to
39 * the given device node.
41 * Return a pointer to the FCP instance, or an ERR_PTR if the instance can't be
44 struct rcar_fcp_device *rcar_fcp_get(const struct device_node *np)
46 struct rcar_fcp_device *fcp;
48 mutex_lock(&fcp_lock);
50 list_for_each_entry(fcp, &fcp_devices, list) {
51 if (fcp->dev->of_node != np)
58 fcp = ERR_PTR(-EPROBE_DEFER);
61 mutex_unlock(&fcp_lock);
64 EXPORT_SYMBOL_GPL(rcar_fcp_get);
67 * rcar_fcp_put - Release a reference to an FCP instance
68 * @fcp: The FCP instance
70 * Release the FCP instance acquired by a call to rcar_fcp_get().
72 void rcar_fcp_put(struct rcar_fcp_device *fcp)
77 EXPORT_SYMBOL_GPL(rcar_fcp_put);
79 struct device *rcar_fcp_get_device(struct rcar_fcp_device *fcp)
83 EXPORT_SYMBOL_GPL(rcar_fcp_get_device);
86 * rcar_fcp_enable - Enable an FCP
87 * @fcp: The FCP instance
89 * Before any memory access through an FCP is performed by a module, the FCP
90 * must be enabled by a call to this function. The enable calls are reference
91 * counted, each successful call must be followed by one rcar_fcp_disable()
92 * call when no more memory transfer can occur through the FCP.
94 * Return 0 on success or a negative error code if an error occurs. The enable
95 * reference count isn't increased when this function returns an error.
97 int rcar_fcp_enable(struct rcar_fcp_device *fcp)
102 return pm_runtime_resume_and_get(fcp->dev);
104 EXPORT_SYMBOL_GPL(rcar_fcp_enable);
107 * rcar_fcp_disable - Disable an FCP
108 * @fcp: The FCP instance
110 * This function is the counterpart of rcar_fcp_enable(). As enable calls are
111 * reference counted a disable call may not disable the FCP synchronously.
113 void rcar_fcp_disable(struct rcar_fcp_device *fcp)
116 pm_runtime_put(fcp->dev);
118 EXPORT_SYMBOL_GPL(rcar_fcp_disable);
120 /* -----------------------------------------------------------------------------
124 static int rcar_fcp_probe(struct platform_device *pdev)
126 struct rcar_fcp_device *fcp;
128 fcp = devm_kzalloc(&pdev->dev, sizeof(*fcp), GFP_KERNEL);
132 fcp->dev = &pdev->dev;
134 dma_set_max_seg_size(fcp->dev, UINT_MAX);
136 pm_runtime_enable(&pdev->dev);
138 mutex_lock(&fcp_lock);
139 list_add_tail(&fcp->list, &fcp_devices);
140 mutex_unlock(&fcp_lock);
142 platform_set_drvdata(pdev, fcp);
147 static void rcar_fcp_remove(struct platform_device *pdev)
149 struct rcar_fcp_device *fcp = platform_get_drvdata(pdev);
151 mutex_lock(&fcp_lock);
152 list_del(&fcp->list);
153 mutex_unlock(&fcp_lock);
155 pm_runtime_disable(&pdev->dev);
158 static const struct of_device_id rcar_fcp_of_match[] = {
159 { .compatible = "renesas,fcpf" },
160 { .compatible = "renesas,fcpv" },
163 MODULE_DEVICE_TABLE(of, rcar_fcp_of_match);
165 static struct platform_driver rcar_fcp_platform_driver = {
166 .probe = rcar_fcp_probe,
167 .remove_new = rcar_fcp_remove,
170 .of_match_table = rcar_fcp_of_match,
171 .suppress_bind_attrs = true,
175 module_platform_driver(rcar_fcp_platform_driver);
177 MODULE_ALIAS("rcar-fcp");
179 MODULE_DESCRIPTION("Renesas FCP Driver");
180 MODULE_LICENSE("GPL");