Commit | Line | Data |
---|---|---|
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 | ||
34 | static int rvin_mbus_supported(struct rvin_dev *vin) | |
35 | { | |
36 | struct v4l2_subdev *sd; | |
37 | struct v4l2_subdev_mbus_code_enum code = { | |
38 | .which = V4L2_SUBDEV_FORMAT_ACTIVE, | |
39 | }; | |
40 | ||
41 | sd = vin_to_source(vin); | |
42 | ||
43 | code.index = 0; | |
44 | while (!v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code)) { | |
45 | code.index++; | |
46 | switch (code.code) { | |
47 | case MEDIA_BUS_FMT_YUYV8_1X16: | |
48 | case MEDIA_BUS_FMT_YUYV8_2X8: | |
49 | case MEDIA_BUS_FMT_YUYV10_2X10: | |
50 | case MEDIA_BUS_FMT_RGB888_1X24: | |
51 | vin->source.code = code.code; | |
52 | vin_dbg(vin, "Found supported media bus format: %d\n", | |
53 | vin->source.code); | |
54 | return true; | |
55 | default: | |
56 | break; | |
57 | } | |
58 | } | |
59 | ||
60 | return false; | |
61 | } | |
62 | ||
63 | static int rvin_graph_notify_complete(struct v4l2_async_notifier *notifier) | |
64 | { | |
f00add96 NS |
65 | struct rvin_dev *vin = notifier_to_vin(notifier); |
66 | int ret; | |
67 | ||
f00add96 NS |
68 | ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev); |
69 | if (ret < 0) { | |
70 | vin_err(vin, "Failed to register subdev nodes\n"); | |
71 | return ret; | |
72 | } | |
73 | ||
74 | if (!rvin_mbus_supported(vin)) { | |
75 | vin_err(vin, "No supported mediabus format found\n"); | |
76 | return -EINVAL; | |
77 | } | |
78 | ||
79 | return rvin_v4l2_probe(vin); | |
80 | } | |
81 | ||
82 | static void rvin_graph_notify_unbind(struct v4l2_async_notifier *notifier, | |
83 | struct v4l2_subdev *sd, | |
84 | struct v4l2_async_subdev *asd) | |
85 | { | |
86 | struct rvin_dev *vin = notifier_to_vin(notifier); | |
87 | ||
88 | rvin_v4l2_remove(vin); | |
89 | } | |
90 | ||
91 | static int rvin_graph_notify_bound(struct v4l2_async_notifier *notifier, | |
92 | struct v4l2_subdev *subdev, | |
93 | struct v4l2_async_subdev *asd) | |
94 | { | |
95 | struct rvin_dev *vin = notifier_to_vin(notifier); | |
96 | ||
97 | vin_dbg(vin, "subdev %s bound\n", subdev->name); | |
98 | ||
99 | vin->entity.entity = &subdev->entity; | |
100 | vin->entity.subdev = subdev; | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
105 | static int rvin_graph_parse(struct rvin_dev *vin, | |
106 | struct device_node *node) | |
107 | { | |
108 | struct device_node *remote; | |
109 | struct device_node *ep = NULL; | |
110 | struct device_node *next; | |
111 | int ret = 0; | |
112 | ||
113 | while (1) { | |
114 | next = of_graph_get_next_endpoint(node, ep); | |
115 | if (!next) | |
116 | break; | |
117 | ||
118 | of_node_put(ep); | |
119 | ep = next; | |
120 | ||
121 | remote = of_graph_get_remote_port_parent(ep); | |
122 | if (!remote) { | |
123 | ret = -EINVAL; | |
124 | break; | |
125 | } | |
126 | ||
127 | /* Skip entities that we have already processed. */ | |
128 | if (remote == vin->dev->of_node) { | |
129 | of_node_put(remote); | |
130 | continue; | |
131 | } | |
132 | ||
133 | /* Remote node to connect */ | |
134 | if (!vin->entity.node) { | |
135 | vin->entity.node = remote; | |
136 | vin->entity.asd.match_type = V4L2_ASYNC_MATCH_OF; | |
137 | vin->entity.asd.match.of.node = remote; | |
138 | ret++; | |
139 | } | |
140 | } | |
141 | ||
142 | of_node_put(ep); | |
143 | ||
144 | return ret; | |
145 | } | |
146 | ||
147 | static int rvin_graph_init(struct rvin_dev *vin) | |
148 | { | |
149 | struct v4l2_async_subdev **subdevs = NULL; | |
150 | int ret; | |
151 | ||
152 | /* Parse the graph to extract a list of subdevice DT nodes. */ | |
153 | ret = rvin_graph_parse(vin, vin->dev->of_node); | |
154 | if (ret < 0) { | |
155 | vin_err(vin, "Graph parsing failed\n"); | |
156 | goto done; | |
157 | } | |
158 | ||
159 | if (!ret) { | |
160 | vin_err(vin, "No subdev found in graph\n"); | |
161 | goto done; | |
162 | } | |
163 | ||
164 | if (ret != 1) { | |
165 | vin_err(vin, "More then one subdev found in graph\n"); | |
166 | goto done; | |
167 | } | |
168 | ||
169 | /* Register the subdevices notifier. */ | |
170 | subdevs = devm_kzalloc(vin->dev, sizeof(*subdevs), GFP_KERNEL); | |
171 | if (subdevs == NULL) { | |
172 | ret = -ENOMEM; | |
173 | goto done; | |
174 | } | |
175 | ||
176 | subdevs[0] = &vin->entity.asd; | |
177 | ||
178 | vin->notifier.subdevs = subdevs; | |
179 | vin->notifier.num_subdevs = 1; | |
180 | vin->notifier.bound = rvin_graph_notify_bound; | |
181 | vin->notifier.unbind = rvin_graph_notify_unbind; | |
182 | vin->notifier.complete = rvin_graph_notify_complete; | |
183 | ||
184 | ret = v4l2_async_notifier_register(&vin->v4l2_dev, &vin->notifier); | |
185 | if (ret < 0) { | |
186 | vin_err(vin, "Notifier registration failed\n"); | |
187 | goto done; | |
188 | } | |
189 | ||
190 | ret = 0; | |
191 | ||
192 | done: | |
193 | if (ret < 0) { | |
194 | v4l2_async_notifier_unregister(&vin->notifier); | |
195 | of_node_put(vin->entity.node); | |
196 | } | |
197 | ||
198 | return ret; | |
199 | } | |
200 | ||
201 | /* ----------------------------------------------------------------------------- | |
202 | * Platform Device Driver | |
203 | */ | |
204 | ||
205 | static const struct of_device_id rvin_of_id_table[] = { | |
206 | { .compatible = "renesas,vin-r8a7794", .data = (void *)RCAR_GEN2 }, | |
207 | { .compatible = "renesas,vin-r8a7793", .data = (void *)RCAR_GEN2 }, | |
208 | { .compatible = "renesas,vin-r8a7791", .data = (void *)RCAR_GEN2 }, | |
209 | { .compatible = "renesas,vin-r8a7790", .data = (void *)RCAR_GEN2 }, | |
210 | { .compatible = "renesas,vin-r8a7779", .data = (void *)RCAR_H1 }, | |
211 | { .compatible = "renesas,vin-r8a7778", .data = (void *)RCAR_M1 }, | |
212 | { }, | |
213 | }; | |
214 | MODULE_DEVICE_TABLE(of, rvin_of_id_table); | |
215 | ||
216 | static int rvin_parse_dt(struct rvin_dev *vin) | |
217 | { | |
218 | const struct of_device_id *match; | |
219 | struct v4l2_of_endpoint ep; | |
220 | struct device_node *np; | |
221 | int ret; | |
222 | ||
223 | match = of_match_device(of_match_ptr(rvin_of_id_table), vin->dev); | |
224 | if (!match) | |
225 | return -ENODEV; | |
226 | ||
227 | vin->chip = (enum chip_id)match->data; | |
228 | ||
229 | np = of_graph_get_next_endpoint(vin->dev->of_node, NULL); | |
230 | if (!np) { | |
231 | vin_err(vin, "Could not find endpoint\n"); | |
232 | return -EINVAL; | |
233 | } | |
234 | ||
235 | ret = v4l2_of_parse_endpoint(np, &ep); | |
236 | if (ret) { | |
237 | vin_err(vin, "Could not parse endpoint\n"); | |
238 | return ret; | |
239 | } | |
240 | ||
241 | of_node_put(np); | |
242 | ||
243 | vin->mbus_cfg.type = ep.bus_type; | |
244 | ||
245 | switch (vin->mbus_cfg.type) { | |
246 | case V4L2_MBUS_PARALLEL: | |
247 | vin->mbus_cfg.flags = ep.bus.parallel.flags; | |
248 | break; | |
249 | case V4L2_MBUS_BT656: | |
250 | vin->mbus_cfg.flags = 0; | |
251 | break; | |
252 | default: | |
253 | vin_err(vin, "Unknown media bus type\n"); | |
254 | return -EINVAL; | |
255 | } | |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
260 | static int rcar_vin_probe(struct platform_device *pdev) | |
261 | { | |
262 | struct rvin_dev *vin; | |
263 | struct resource *mem; | |
264 | int irq, ret; | |
265 | ||
266 | vin = devm_kzalloc(&pdev->dev, sizeof(*vin), GFP_KERNEL); | |
267 | if (!vin) | |
268 | return -ENOMEM; | |
269 | ||
270 | vin->dev = &pdev->dev; | |
271 | ||
272 | ret = rvin_parse_dt(vin); | |
273 | if (ret) | |
274 | return ret; | |
275 | ||
276 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
277 | if (mem == NULL) | |
278 | return -EINVAL; | |
279 | ||
280 | vin->base = devm_ioremap_resource(vin->dev, mem); | |
281 | if (IS_ERR(vin->base)) | |
282 | return PTR_ERR(vin->base); | |
283 | ||
284 | irq = platform_get_irq(pdev, 0); | |
285 | if (irq <= 0) | |
286 | return ret; | |
287 | ||
288 | ret = rvin_dma_probe(vin, irq); | |
289 | if (ret) | |
290 | return ret; | |
291 | ||
292 | ret = rvin_graph_init(vin); | |
293 | if (ret < 0) | |
294 | goto error; | |
295 | ||
296 | pm_suspend_ignore_children(&pdev->dev, true); | |
297 | pm_runtime_enable(&pdev->dev); | |
298 | ||
299 | platform_set_drvdata(pdev, vin); | |
300 | ||
301 | return 0; | |
302 | error: | |
303 | rvin_dma_remove(vin); | |
304 | ||
305 | return ret; | |
306 | } | |
307 | ||
308 | static int rcar_vin_remove(struct platform_device *pdev) | |
309 | { | |
310 | struct rvin_dev *vin = platform_get_drvdata(pdev); | |
311 | ||
312 | pm_runtime_disable(&pdev->dev); | |
313 | ||
314 | v4l2_async_notifier_unregister(&vin->notifier); | |
315 | ||
316 | rvin_dma_remove(vin); | |
317 | ||
318 | return 0; | |
319 | } | |
320 | ||
321 | static struct platform_driver rcar_vin_driver = { | |
322 | .driver = { | |
323 | .name = "rcar-vin", | |
324 | .of_match_table = rvin_of_id_table, | |
325 | }, | |
326 | .probe = rcar_vin_probe, | |
327 | .remove = rcar_vin_remove, | |
328 | }; | |
329 | ||
330 | module_platform_driver(rcar_vin_driver); | |
331 | ||
332 | MODULE_AUTHOR("Niklas Söderlund <niklas.soderlund@ragnatech.se>"); | |
333 | MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver"); | |
334 | MODULE_LICENSE("GPL v2"); |