Merge remote-tracking branch 'vfio/next'
[deliverable/linux.git] / drivers / media / platform / rcar-vin / rcar-core.c
CommitLineData
f00add96
NS
1/*
2 * Driver for Renesas R-Car VIN
3 *
4 * Copyright (C) 2016 Renesas Electronics Corp.
5 * Copyright (C) 2011-2013 Renesas Solutions Corp.
6 * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
7 * Copyright (C) 2008 Magnus Damm
8 *
9 * Based on the soc-camera rcar_vin driver
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/of_device.h>
20#include <linux/of_graph.h>
21#include <linux/platform_device.h>
22#include <linux/pm_runtime.h>
23
24#include <media/v4l2-of.h>
25
26#include "rcar-vin.h"
27
28/* -----------------------------------------------------------------------------
29 * Async notifier
30 */
31
32#define notifier_to_vin(n) container_of(n, struct rvin_dev, notifier)
33
b50b77e6 34static bool rvin_mbus_supported(struct rvin_graph_entity *entity)
f00add96 35{
b50b77e6 36 struct v4l2_subdev *sd = entity->subdev;
f00add96
NS
37 struct v4l2_subdev_mbus_code_enum code = {
38 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
39 };
40
f00add96
NS
41 code.index = 0;
42 while (!v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code)) {
43 code.index++;
44 switch (code.code) {
45 case MEDIA_BUS_FMT_YUYV8_1X16:
46 case MEDIA_BUS_FMT_YUYV8_2X8:
47 case MEDIA_BUS_FMT_YUYV10_2X10:
48 case MEDIA_BUS_FMT_RGB888_1X24:
b50b77e6 49 entity->code = code.code;
f00add96
NS
50 return true;
51 default:
52 break;
53 }
54 }
55
56 return false;
57}
58
4869ce9d 59static int rvin_digital_notify_complete(struct v4l2_async_notifier *notifier)
f00add96 60{
f00add96
NS
61 struct rvin_dev *vin = notifier_to_vin(notifier);
62 int ret;
63
83fba2c0 64 /* Verify subdevices mbus format */
b50b77e6 65 if (!rvin_mbus_supported(&vin->digital)) {
83fba2c0
NS
66 vin_err(vin, "Unsupported media bus format for %s\n",
67 vin->digital.subdev->name);
68 return -EINVAL;
69 }
70
71 vin_dbg(vin, "Found media bus format for %s: %d\n",
b50b77e6 72 vin->digital.subdev->name, vin->digital.code);
83fba2c0 73
f00add96
NS
74 ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
75 if (ret < 0) {
76 vin_err(vin, "Failed to register subdev nodes\n");
77 return ret;
78 }
79
f00add96
NS
80 return rvin_v4l2_probe(vin);
81}
82
4869ce9d
NS
83static void rvin_digital_notify_unbind(struct v4l2_async_notifier *notifier,
84 struct v4l2_subdev *subdev,
85 struct v4l2_async_subdev *asd)
f00add96
NS
86{
87 struct rvin_dev *vin = notifier_to_vin(notifier);
88
83fba2c0
NS
89 if (vin->digital.subdev == subdev) {
90 vin_dbg(vin, "unbind digital subdev %s\n", subdev->name);
91 rvin_v4l2_remove(vin);
92 vin->digital.subdev = NULL;
93 return;
94 }
95
96 vin_err(vin, "no entity for subdev %s to unbind\n", subdev->name);
f00add96
NS
97}
98
4869ce9d
NS
99static int rvin_digital_notify_bound(struct v4l2_async_notifier *notifier,
100 struct v4l2_subdev *subdev,
101 struct v4l2_async_subdev *asd)
f00add96
NS
102{
103 struct rvin_dev *vin = notifier_to_vin(notifier);
104
83fba2c0 105 v4l2_set_subdev_hostdata(subdev, vin);
f00add96 106
83fba2c0
NS
107 if (vin->digital.asd.match.of.node == subdev->dev->of_node) {
108 vin_dbg(vin, "bound digital subdev %s\n", subdev->name);
109 vin->digital.subdev = subdev;
110 return 0;
111 }
f00add96 112
83fba2c0
NS
113 vin_err(vin, "no entity for subdev %s to bind\n", subdev->name);
114 return -EINVAL;
f00add96
NS
115}
116
83fba2c0
NS
117static int rvin_digitial_parse_v4l2(struct rvin_dev *vin,
118 struct device_node *ep,
119 struct v4l2_mbus_config *mbus_cfg)
f00add96 120{
83fba2c0
NS
121 struct v4l2_of_endpoint v4l2_ep;
122 int ret;
f00add96 123
83fba2c0
NS
124 ret = v4l2_of_parse_endpoint(ep, &v4l2_ep);
125 if (ret) {
126 vin_err(vin, "Could not parse v4l2 endpoint\n");
127 return -EINVAL;
128 }
f00add96 129
83fba2c0 130 mbus_cfg->type = v4l2_ep.bus_type;
f00add96 131
83fba2c0
NS
132 switch (mbus_cfg->type) {
133 case V4L2_MBUS_PARALLEL:
134 vin_dbg(vin, "Found PARALLEL media bus\n");
135 mbus_cfg->flags = v4l2_ep.bus.parallel.flags;
136 break;
137 case V4L2_MBUS_BT656:
138 vin_dbg(vin, "Found BT656 media bus\n");
139 mbus_cfg->flags = 0;
140 break;
141 default:
142 vin_err(vin, "Unknown media bus type\n");
143 return -EINVAL;
144 }
f00add96 145
83fba2c0
NS
146 return 0;
147}
148
149static int rvin_digital_graph_parse(struct rvin_dev *vin)
150{
151 struct device_node *ep, *np;
152 int ret;
153
154 vin->digital.asd.match.of.node = NULL;
155 vin->digital.subdev = NULL;
156
157 /*
158 * Port 0 id 0 is local digital input, try to get it.
159 * Not all instances can or will have this, that is OK
160 */
161 ep = of_graph_get_endpoint_by_regs(vin->dev->of_node, 0, 0);
162 if (!ep)
163 return 0;
164
165 np = of_graph_get_remote_port_parent(ep);
166 if (!np) {
167 vin_err(vin, "No remote parent for digital input\n");
168 of_node_put(ep);
169 return -EINVAL;
f00add96 170 }
83fba2c0 171 of_node_put(np);
f00add96 172
b50b77e6 173 ret = rvin_digitial_parse_v4l2(vin, ep, &vin->digital.mbus_cfg);
f00add96 174 of_node_put(ep);
83fba2c0
NS
175 if (ret)
176 return ret;
f00add96 177
83fba2c0
NS
178 vin->digital.asd.match.of.node = np;
179 vin->digital.asd.match_type = V4L2_ASYNC_MATCH_OF;
180
181 return 0;
f00add96
NS
182}
183
83fba2c0 184static int rvin_digital_graph_init(struct rvin_dev *vin)
f00add96
NS
185{
186 struct v4l2_async_subdev **subdevs = NULL;
187 int ret;
188
83fba2c0
NS
189 ret = rvin_digital_graph_parse(vin);
190 if (ret)
191 return ret;
f00add96 192
83fba2c0
NS
193 if (!vin->digital.asd.match.of.node) {
194 vin_dbg(vin, "No digital subdevice found\n");
195 return -ENODEV;
f00add96
NS
196 }
197
198 /* Register the subdevices notifier. */
199 subdevs = devm_kzalloc(vin->dev, sizeof(*subdevs), GFP_KERNEL);
83fba2c0
NS
200 if (subdevs == NULL)
201 return -ENOMEM;
f00add96 202
4869ce9d 203 subdevs[0] = &vin->digital.asd;
f00add96 204
83fba2c0
NS
205 vin_dbg(vin, "Found digital subdevice %s\n",
206 of_node_full_name(subdevs[0]->match.of.node));
207
f00add96 208 vin->notifier.num_subdevs = 1;
83fba2c0 209 vin->notifier.subdevs = subdevs;
4869ce9d
NS
210 vin->notifier.bound = rvin_digital_notify_bound;
211 vin->notifier.unbind = rvin_digital_notify_unbind;
212 vin->notifier.complete = rvin_digital_notify_complete;
f00add96
NS
213
214 ret = v4l2_async_notifier_register(&vin->v4l2_dev, &vin->notifier);
215 if (ret < 0) {
216 vin_err(vin, "Notifier registration failed\n");
83fba2c0 217 return ret;
f00add96
NS
218 }
219
83fba2c0 220 return 0;
f00add96
NS
221}
222
223/* -----------------------------------------------------------------------------
224 * Platform Device Driver
225 */
226
227static const struct of_device_id rvin_of_id_table[] = {
228 { .compatible = "renesas,vin-r8a7794", .data = (void *)RCAR_GEN2 },
229 { .compatible = "renesas,vin-r8a7793", .data = (void *)RCAR_GEN2 },
230 { .compatible = "renesas,vin-r8a7791", .data = (void *)RCAR_GEN2 },
231 { .compatible = "renesas,vin-r8a7790", .data = (void *)RCAR_GEN2 },
232 { .compatible = "renesas,vin-r8a7779", .data = (void *)RCAR_H1 },
233 { .compatible = "renesas,vin-r8a7778", .data = (void *)RCAR_M1 },
234 { },
235};
236MODULE_DEVICE_TABLE(of, rvin_of_id_table);
237
f00add96
NS
238static int rcar_vin_probe(struct platform_device *pdev)
239{
83fba2c0 240 const struct of_device_id *match;
f00add96
NS
241 struct rvin_dev *vin;
242 struct resource *mem;
243 int irq, ret;
244
245 vin = devm_kzalloc(&pdev->dev, sizeof(*vin), GFP_KERNEL);
246 if (!vin)
247 return -ENOMEM;
248
83fba2c0
NS
249 match = of_match_device(of_match_ptr(rvin_of_id_table), &pdev->dev);
250 if (!match)
251 return -ENODEV;
f00add96 252
83fba2c0
NS
253 vin->dev = &pdev->dev;
254 vin->chip = (enum chip_id)match->data;
f00add96
NS
255
256 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
257 if (mem == NULL)
258 return -EINVAL;
259
260 vin->base = devm_ioremap_resource(vin->dev, mem);
261 if (IS_ERR(vin->base))
262 return PTR_ERR(vin->base);
263
264 irq = platform_get_irq(pdev, 0);
fc738177
NS
265 if (irq < 0)
266 return irq;
f00add96
NS
267
268 ret = rvin_dma_probe(vin, irq);
269 if (ret)
270 return ret;
271
83fba2c0 272 ret = rvin_digital_graph_init(vin);
f00add96
NS
273 if (ret < 0)
274 goto error;
275
276 pm_suspend_ignore_children(&pdev->dev, true);
277 pm_runtime_enable(&pdev->dev);
278
279 platform_set_drvdata(pdev, vin);
280
281 return 0;
282error:
283 rvin_dma_remove(vin);
284
285 return ret;
286}
287
288static int rcar_vin_remove(struct platform_device *pdev)
289{
290 struct rvin_dev *vin = platform_get_drvdata(pdev);
291
292 pm_runtime_disable(&pdev->dev);
293
294 v4l2_async_notifier_unregister(&vin->notifier);
295
296 rvin_dma_remove(vin);
297
298 return 0;
299}
300
301static struct platform_driver rcar_vin_driver = {
302 .driver = {
303 .name = "rcar-vin",
304 .of_match_table = rvin_of_id_table,
305 },
306 .probe = rcar_vin_probe,
307 .remove = rcar_vin_remove,
308};
309
310module_platform_driver(rcar_vin_driver);
311
312MODULE_AUTHOR("Niklas Söderlund <niklas.soderlund@ragnatech.se>");
313MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver");
314MODULE_LICENSE("GPL v2");
This page took 0.0548959999999999 seconds and 5 git commands to generate.