1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * S5P/EXYNOS4 SoC series camera host interface media device driver
4 *
5 * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
6 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
7 */
8
9 #include <linux/bug.h>
10 #include <linux/clk.h>
11 #include <linux/clk-provider.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/i2c.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_platform.h>
20 #include <linux/of_graph.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <media/v4l2-async.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-fwnode.h>
29 #include <media/media-device.h>
30 #include <media/drv-intf/exynos-fimc.h>
31
32 #include "media-dev.h"
33 #include "fimc-core.h"
34 #include "fimc-is.h"
35 #include "fimc-lite.h"
36 #include "mipi-csis.h"
37
38 /* Set up image sensor subdev -> FIMC capture node notifications. */
__setup_sensor_notification(struct fimc_md * fmd,struct v4l2_subdev * sensor,struct v4l2_subdev * fimc_sd)39 static void __setup_sensor_notification(struct fimc_md *fmd,
40 struct v4l2_subdev *sensor,
41 struct v4l2_subdev *fimc_sd)
42 {
43 struct fimc_source_info *src_inf;
44 struct fimc_sensor_info *md_si;
45 unsigned long flags;
46
47 src_inf = v4l2_get_subdev_hostdata(sensor);
48 if (!src_inf || WARN_ON(fmd == NULL))
49 return;
50
51 md_si = source_to_sensor_info(src_inf);
52 spin_lock_irqsave(&fmd->slock, flags);
53 md_si->host = v4l2_get_subdevdata(fimc_sd);
54 spin_unlock_irqrestore(&fmd->slock, flags);
55 }
56
57 /**
58 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
59 * @p: fimc pipeline
60 * @me: media entity terminating the pipeline
61 *
62 * Caller holds the graph mutex.
63 */
fimc_pipeline_prepare(struct fimc_pipeline * p,struct media_entity * me)64 static void fimc_pipeline_prepare(struct fimc_pipeline *p,
65 struct media_entity *me)
66 {
67 struct fimc_md *fmd = entity_to_fimc_mdev(me);
68 struct v4l2_subdev *sd;
69 struct v4l2_subdev *sensor = NULL;
70 int i;
71
72 for (i = 0; i < IDX_MAX; i++)
73 p->subdevs[i] = NULL;
74
75 while (1) {
76 struct media_pad *pad = NULL;
77
78 /* Find remote source pad */
79 for (i = 0; i < me->num_pads; i++) {
80 struct media_pad *spad = &me->pads[i];
81 if (!(spad->flags & MEDIA_PAD_FL_SINK))
82 continue;
83 pad = media_pad_remote_pad_first(spad);
84 if (pad)
85 break;
86 }
87
88 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
89 break;
90 sd = media_entity_to_v4l2_subdev(pad->entity);
91
92 switch (sd->grp_id) {
93 case GRP_ID_SENSOR:
94 sensor = sd;
95 fallthrough;
96 case GRP_ID_FIMC_IS_SENSOR:
97 p->subdevs[IDX_SENSOR] = sd;
98 break;
99 case GRP_ID_CSIS:
100 p->subdevs[IDX_CSIS] = sd;
101 break;
102 case GRP_ID_FLITE:
103 p->subdevs[IDX_FLITE] = sd;
104 break;
105 case GRP_ID_FIMC:
106 p->subdevs[IDX_FIMC] = sd;
107 break;
108 case GRP_ID_FIMC_IS:
109 p->subdevs[IDX_IS_ISP] = sd;
110 break;
111 default:
112 break;
113 }
114 me = &sd->entity;
115 if (me->num_pads == 1)
116 break;
117 }
118
119 if (sensor && p->subdevs[IDX_FIMC])
120 __setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
121 }
122
123 /**
124 * __subdev_set_power - change power state of a single subdev
125 * @sd: subdevice to change power state for
126 * @on: 1 to enable power or 0 to disable
127 *
128 * Return result of s_power subdev operation or -ENXIO if sd argument
129 * is NULL. Return 0 if the subdevice does not implement s_power.
130 */
__subdev_set_power(struct v4l2_subdev * sd,int on)131 static int __subdev_set_power(struct v4l2_subdev *sd, int on)
132 {
133 int *use_count;
134 int ret;
135
136 if (sd == NULL)
137 return -ENXIO;
138
139 use_count = &sd->entity.use_count;
140 if (on && (*use_count)++ > 0)
141 return 0;
142 else if (!on && (*use_count == 0 || --(*use_count) > 0))
143 return 0;
144 ret = v4l2_subdev_call(sd, core, s_power, on);
145
146 return ret != -ENOIOCTLCMD ? ret : 0;
147 }
148
149 /**
150 * fimc_pipeline_s_power - change power state of all pipeline subdevs
151 * @p: fimc device terminating the pipeline
152 * @on: true to power on, false to power off
153 *
154 * Needs to be called with the graph mutex held.
155 */
fimc_pipeline_s_power(struct fimc_pipeline * p,bool on)156 static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
157 {
158 static const u8 seq[2][IDX_MAX - 1] = {
159 { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
160 { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
161 };
162 int i, ret = 0;
163
164 if (p->subdevs[IDX_SENSOR] == NULL)
165 return -ENXIO;
166
167 for (i = 0; i < IDX_MAX - 1; i++) {
168 unsigned int idx = seq[on][i];
169
170 ret = __subdev_set_power(p->subdevs[idx], on);
171
172
173 if (ret < 0 && ret != -ENXIO)
174 goto error;
175 }
176 return 0;
177 error:
178 for (; i >= 0; i--) {
179 unsigned int idx = seq[on][i];
180 __subdev_set_power(p->subdevs[idx], !on);
181 }
182 return ret;
183 }
184
185 /**
186 * __fimc_pipeline_enable - enable power of all pipeline subdevs
187 * and the sensor clock
188 * @ep: video pipeline structure
189 * @fmd: fimc media device
190 *
191 * Called with the graph mutex held.
192 */
__fimc_pipeline_enable(struct exynos_media_pipeline * ep,struct fimc_md * fmd)193 static int __fimc_pipeline_enable(struct exynos_media_pipeline *ep,
194 struct fimc_md *fmd)
195 {
196 struct fimc_pipeline *p = to_fimc_pipeline(ep);
197 int ret;
198
199 /* Enable PXLASYNC clock if this pipeline includes FIMC-IS */
200 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
201 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
202 if (ret < 0)
203 return ret;
204 }
205
206 ret = fimc_pipeline_s_power(p, 1);
207 if (!ret)
208 return 0;
209
210 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
211 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
212
213 return ret;
214 }
215
216 /**
217 * __fimc_pipeline_open - update the pipeline information, enable power
218 * of all pipeline subdevs and the sensor clock
219 * @ep: fimc device terminating the pipeline
220 * @me: media entity to start graph walk with
221 * @prepare: true to walk the current pipeline and acquire all subdevs
222 *
223 * Called with the graph mutex held.
224 */
__fimc_pipeline_open(struct exynos_media_pipeline * ep,struct media_entity * me,bool prepare)225 static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
226 struct media_entity *me, bool prepare)
227 {
228 struct fimc_md *fmd = entity_to_fimc_mdev(me);
229 struct fimc_pipeline *p = to_fimc_pipeline(ep);
230 struct v4l2_subdev *sd;
231
232 if (WARN_ON(p == NULL || me == NULL))
233 return -EINVAL;
234
235 if (prepare)
236 fimc_pipeline_prepare(p, me);
237
238 sd = p->subdevs[IDX_SENSOR];
239 if (sd == NULL) {
240 pr_warn("%s(): No sensor subdev\n", __func__);
241 /*
242 * Pipeline open cannot fail so as to make it possible
243 * for the user space to configure the pipeline.
244 */
245 return 0;
246 }
247
248 return __fimc_pipeline_enable(ep, fmd);
249 }
250
251 /**
252 * __fimc_pipeline_close - disable the sensor clock and pipeline power
253 * @ep: fimc device terminating the pipeline
254 *
255 * Disable power of all subdevs and turn the external sensor clock off.
256 */
__fimc_pipeline_close(struct exynos_media_pipeline * ep)257 static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
258 {
259 struct fimc_pipeline *p = to_fimc_pipeline(ep);
260 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
261 struct fimc_md *fmd;
262 int ret;
263
264 if (sd == NULL) {
265 pr_warn("%s(): No sensor subdev\n", __func__);
266 return 0;
267 }
268
269 ret = fimc_pipeline_s_power(p, 0);
270
271 fmd = entity_to_fimc_mdev(&sd->entity);
272
273 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
274 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
275 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
276
277 return ret == -ENXIO ? 0 : ret;
278 }
279
280 /**
281 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
282 * @ep: video pipeline structure
283 * @on: passed as the s_stream() callback argument
284 */
__fimc_pipeline_s_stream(struct exynos_media_pipeline * ep,bool on)285 static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
286 {
287 static const u8 seq[2][IDX_MAX] = {
288 { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
289 { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
290 };
291 struct fimc_pipeline *p = to_fimc_pipeline(ep);
292 enum fimc_subdev_index sd_id;
293 int i, ret = 0;
294
295 if (p->subdevs[IDX_SENSOR] == NULL) {
296 struct fimc_md *fmd;
297 struct v4l2_subdev *sd = p->subdevs[IDX_CSIS];
298
299 if (!sd)
300 sd = p->subdevs[IDX_FIMC];
301
302 if (!sd) {
303 /*
304 * If neither CSIS nor FIMC was set up,
305 * it's impossible to have any sensors
306 */
307 return -ENODEV;
308 }
309
310 fmd = entity_to_fimc_mdev(&sd->entity);
311
312 if (!fmd->user_subdev_api) {
313 /*
314 * Sensor must be already discovered if we
315 * aren't in the user_subdev_api mode
316 */
317 return -ENODEV;
318 }
319
320 /* Get pipeline sink entity */
321 if (p->subdevs[IDX_FIMC])
322 sd_id = IDX_FIMC;
323 else if (p->subdevs[IDX_IS_ISP])
324 sd_id = IDX_IS_ISP;
325 else if (p->subdevs[IDX_FLITE])
326 sd_id = IDX_FLITE;
327 else
328 return -ENODEV;
329
330 /*
331 * Sensor could have been linked between open and STREAMON -
332 * check if this is the case.
333 */
334 fimc_pipeline_prepare(p, &p->subdevs[sd_id]->entity);
335
336 if (p->subdevs[IDX_SENSOR] == NULL)
337 return -ENODEV;
338
339 ret = __fimc_pipeline_enable(ep, fmd);
340 if (ret < 0)
341 return ret;
342
343 }
344
345 for (i = 0; i < IDX_MAX; i++) {
346 unsigned int idx = seq[on][i];
347
348 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
349
350 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
351 goto error;
352 }
353
354 return 0;
355 error:
356 fimc_pipeline_s_power(p, !on);
357 for (; i >= 0; i--) {
358 unsigned int idx = seq[on][i];
359 v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
360 }
361 return ret;
362 }
363
364 /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
365 static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
366 .open = __fimc_pipeline_open,
367 .close = __fimc_pipeline_close,
368 .set_stream = __fimc_pipeline_s_stream,
369 };
370
fimc_md_pipeline_create(struct fimc_md * fmd)371 static struct exynos_media_pipeline *fimc_md_pipeline_create(
372 struct fimc_md *fmd)
373 {
374 struct fimc_pipeline *p;
375
376 p = kzalloc_obj(*p);
377 if (!p)
378 return NULL;
379
380 list_add_tail(&p->list, &fmd->pipelines);
381
382 p->ep.ops = &fimc_pipeline_ops;
383 return &p->ep;
384 }
385
fimc_md_pipelines_free(struct fimc_md * fmd)386 static void fimc_md_pipelines_free(struct fimc_md *fmd)
387 {
388 while (!list_empty(&fmd->pipelines)) {
389 struct fimc_pipeline *p;
390
391 p = list_entry(fmd->pipelines.next, typeof(*p), list);
392 list_del(&p->list);
393 kfree(p);
394 }
395 }
396
fimc_md_parse_one_endpoint(struct fimc_md * fmd,struct device_node * ep)397 static int fimc_md_parse_one_endpoint(struct fimc_md *fmd,
398 struct device_node *ep)
399 {
400 int index = fmd->num_sensors;
401 struct fimc_source_info *pd = &fmd->sensor[index].pdata;
402 struct device_node *rem, *np;
403 struct v4l2_async_connection *asd;
404 struct v4l2_fwnode_endpoint endpoint = { .bus_type = 0 };
405 int ret;
406
407 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &endpoint);
408 if (ret) {
409 of_node_put(ep);
410 return ret;
411 }
412
413 if (WARN_ON(endpoint.base.port == 0) || index >= FIMC_MAX_SENSORS) {
414 of_node_put(ep);
415 return -EINVAL;
416 }
417
418 pd->mux_id = (endpoint.base.port - 1) & 0x1;
419
420 rem = of_graph_get_remote_port_parent(ep);
421 if (rem == NULL) {
422 v4l2_info(&fmd->v4l2_dev, "Remote device at %pOF not found\n",
423 ep);
424 of_node_put(ep);
425 return 0;
426 }
427
428 if (fimc_input_is_parallel(endpoint.base.port)) {
429 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
430 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
431 else
432 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
433 pd->flags = endpoint.bus.parallel.flags;
434 } else if (fimc_input_is_mipi_csi(endpoint.base.port)) {
435 /*
436 * MIPI CSI-2: only input mux selection and
437 * the sensor's clock frequency is needed.
438 */
439 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
440 } else {
441 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %pOF\n",
442 endpoint.base.port, rem);
443 }
444 /*
445 * For FIMC-IS handled sensors, that are placed under i2c-isp device
446 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
447 * input. Sensors are attached to the FIMC-LITE hostdata interface
448 * directly or through MIPI-CSIS, depending on the external media bus
449 * used. This needs to be handled in a more reliable way, not by just
450 * checking parent's node name.
451 */
452 np = of_get_parent(rem);
453 of_node_put(rem);
454
455 if (of_node_name_eq(np, "i2c-isp"))
456 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
457 else
458 pd->fimc_bus_type = pd->sensor_bus_type;
459 of_node_put(np);
460
461 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor))) {
462 of_node_put(ep);
463 return -EINVAL;
464 }
465
466 asd = v4l2_async_nf_add_fwnode_remote(&fmd->subdev_notifier,
467 of_fwnode_handle(ep),
468 struct v4l2_async_connection);
469
470 of_node_put(ep);
471
472 if (IS_ERR(asd))
473 return PTR_ERR(asd);
474
475 fmd->sensor[index].asd = asd;
476 fmd->num_sensors++;
477
478 return 0;
479 }
480
481 /* Parse port node and register as a sub-device any sensor specified there. */
fimc_md_parse_port_node(struct fimc_md * fmd,struct device_node * port)482 static int fimc_md_parse_port_node(struct fimc_md *fmd,
483 struct device_node *port)
484 {
485 int ret;
486
487 for_each_child_of_node_scoped(port, ep) {
488 ret = fimc_md_parse_one_endpoint(fmd, ep);
489 if (ret < 0)
490 return ret;
491 }
492
493 return 0;
494 }
495
496 /* Register all SoC external sub-devices */
fimc_md_register_sensor_entities(struct fimc_md * fmd)497 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
498 {
499 struct device_node *parent = fmd->pdev->dev.of_node;
500 struct device_node *ports = NULL;
501 int ret;
502
503 /*
504 * Runtime resume one of the FIMC entities to make sure
505 * the sclk_cam clocks are not globally disabled.
506 */
507 if (!fmd->pmf)
508 return -ENXIO;
509
510 ret = pm_runtime_resume_and_get(fmd->pmf);
511 if (ret < 0)
512 return ret;
513
514 fmd->num_sensors = 0;
515
516 /* Attach sensors linked to MIPI CSI-2 receivers */
517 for_each_available_child_of_node_scoped(parent, node) {
518 struct device_node *port;
519
520 if (!of_node_name_eq(node, "csis"))
521 continue;
522 /* The csis node can have only port subnode. */
523 port = of_get_next_child(node, NULL);
524 if (!port)
525 continue;
526
527 ret = fimc_md_parse_port_node(fmd, port);
528 of_node_put(port);
529 if (ret < 0)
530 goto cleanup;
531 }
532
533 /* Attach sensors listed in the parallel-ports node */
534 ports = of_get_child_by_name(parent, "parallel-ports");
535 if (!ports)
536 goto rpm_put;
537
538 for_each_child_of_node_scoped(ports, node) {
539 ret = fimc_md_parse_port_node(fmd, node);
540 if (ret < 0)
541 goto cleanup;
542 }
543 of_node_put(ports);
544
545 rpm_put:
546 pm_runtime_put(fmd->pmf);
547 return 0;
548
549 cleanup:
550 of_node_put(ports);
551 v4l2_async_nf_cleanup(&fmd->subdev_notifier);
552 pm_runtime_put(fmd->pmf);
553 return ret;
554 }
555
__of_get_csis_id(struct device_node * np)556 static int __of_get_csis_id(struct device_node *np)
557 {
558 u32 reg = 0;
559
560 np = of_get_child_by_name(np, "port");
561 if (!np)
562 return -EINVAL;
563 of_property_read_u32(np, "reg", ®);
564 of_node_put(np);
565 return reg - FIMC_INPUT_MIPI_CSI2_0;
566 }
567
568 /*
569 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
570 */
register_fimc_lite_entity(struct fimc_md * fmd,struct fimc_lite * fimc_lite)571 static int register_fimc_lite_entity(struct fimc_md *fmd,
572 struct fimc_lite *fimc_lite)
573 {
574 struct v4l2_subdev *sd;
575 struct exynos_media_pipeline *ep;
576 int ret;
577
578 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
579 fmd->fimc_lite[fimc_lite->index]))
580 return -EBUSY;
581
582 sd = &fimc_lite->subdev;
583 sd->grp_id = GRP_ID_FLITE;
584
585 ep = fimc_md_pipeline_create(fmd);
586 if (!ep)
587 return -ENOMEM;
588
589 v4l2_set_subdev_hostdata(sd, ep);
590
591 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
592 if (!ret)
593 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
594 else
595 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
596 fimc_lite->index);
597 return ret;
598 }
599
register_fimc_entity(struct fimc_md * fmd,struct fimc_dev * fimc)600 static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
601 {
602 struct v4l2_subdev *sd;
603 struct exynos_media_pipeline *ep;
604 int ret;
605
606 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
607 return -EBUSY;
608
609 sd = &fimc->vid_cap.subdev;
610 sd->grp_id = GRP_ID_FIMC;
611
612 ep = fimc_md_pipeline_create(fmd);
613 if (!ep)
614 return -ENOMEM;
615
616 v4l2_set_subdev_hostdata(sd, ep);
617
618 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
619 if (!ret) {
620 if (!fmd->pmf && fimc->pdev)
621 fmd->pmf = &fimc->pdev->dev;
622 fmd->fimc[fimc->id] = fimc;
623 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
624 } else {
625 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
626 fimc->id, ret);
627 }
628 return ret;
629 }
630
register_csis_entity(struct fimc_md * fmd,struct platform_device * pdev,struct v4l2_subdev * sd)631 static int register_csis_entity(struct fimc_md *fmd,
632 struct platform_device *pdev,
633 struct v4l2_subdev *sd)
634 {
635 struct device_node *node = pdev->dev.of_node;
636 int id, ret;
637
638 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
639
640 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
641 return -ENOENT;
642
643 if (WARN_ON(fmd->csis[id].sd))
644 return -EBUSY;
645
646 sd->grp_id = GRP_ID_CSIS;
647 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
648 if (!ret)
649 fmd->csis[id].sd = sd;
650 else
651 v4l2_err(&fmd->v4l2_dev,
652 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
653 return ret;
654 }
655
register_fimc_is_entity(struct fimc_md * fmd,struct fimc_is * is)656 static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
657 {
658 struct v4l2_subdev *sd = &is->isp.subdev;
659 struct exynos_media_pipeline *ep;
660 int ret;
661
662 /* Allocate pipeline object for the ISP capture video node. */
663 ep = fimc_md_pipeline_create(fmd);
664 if (!ep)
665 return -ENOMEM;
666
667 v4l2_set_subdev_hostdata(sd, ep);
668
669 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
670 if (ret) {
671 v4l2_err(&fmd->v4l2_dev,
672 "Failed to register FIMC-ISP (%d)\n", ret);
673 return ret;
674 }
675
676 fmd->fimc_is = is;
677 return 0;
678 }
679
fimc_md_register_platform_entity(struct fimc_md * fmd,struct platform_device * pdev,int plat_entity)680 static int fimc_md_register_platform_entity(struct fimc_md *fmd,
681 struct platform_device *pdev,
682 int plat_entity)
683 {
684 struct device *dev = &pdev->dev;
685 int ret = -EPROBE_DEFER;
686 void *drvdata;
687
688 /* Lock to ensure dev->driver won't change. */
689 device_lock(dev);
690
691 if (!dev->driver || !try_module_get(dev->driver->owner))
692 goto dev_unlock;
693
694 drvdata = dev_get_drvdata(dev);
695 /* Some subdev didn't probe successfully id drvdata is NULL */
696 if (drvdata) {
697 switch (plat_entity) {
698 case IDX_FIMC:
699 ret = register_fimc_entity(fmd, drvdata);
700 break;
701 case IDX_FLITE:
702 ret = register_fimc_lite_entity(fmd, drvdata);
703 break;
704 case IDX_CSIS:
705 ret = register_csis_entity(fmd, pdev, drvdata);
706 break;
707 case IDX_IS_ISP:
708 ret = register_fimc_is_entity(fmd, drvdata);
709 break;
710 default:
711 ret = -ENODEV;
712 }
713 }
714
715 module_put(dev->driver->owner);
716 dev_unlock:
717 device_unlock(dev);
718 if (ret == -EPROBE_DEFER)
719 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
720 dev_name(dev));
721 else if (ret < 0)
722 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
723 dev_name(dev), ret);
724 return ret;
725 }
726
727 /* Register FIMC, FIMC-LITE and CSIS media entities */
fimc_md_register_platform_entities(struct fimc_md * fmd,struct device_node * parent)728 static int fimc_md_register_platform_entities(struct fimc_md *fmd,
729 struct device_node *parent)
730 {
731 int ret = 0;
732
733 for_each_available_child_of_node_scoped(parent, node) {
734 struct platform_device *pdev;
735 int plat_entity = -1;
736
737 pdev = of_find_device_by_node(node);
738 if (!pdev)
739 continue;
740
741 /* If driver of any entity isn't ready try all again later. */
742 if (of_node_name_eq(node, CSIS_OF_NODE_NAME))
743 plat_entity = IDX_CSIS;
744 else if (of_node_name_eq(node, FIMC_IS_OF_NODE_NAME))
745 plat_entity = IDX_IS_ISP;
746 else if (of_node_name_eq(node, FIMC_LITE_OF_NODE_NAME))
747 plat_entity = IDX_FLITE;
748 else if (of_node_name_eq(node, FIMC_OF_NODE_NAME) &&
749 !of_property_read_bool(node, "samsung,lcd-wb"))
750 plat_entity = IDX_FIMC;
751
752 if (plat_entity >= 0)
753 ret = fimc_md_register_platform_entity(fmd, pdev,
754 plat_entity);
755 put_device(&pdev->dev);
756 if (ret < 0)
757 break;
758 }
759
760 return ret;
761 }
762
fimc_md_unregister_entities(struct fimc_md * fmd)763 static void fimc_md_unregister_entities(struct fimc_md *fmd)
764 {
765 int i;
766
767 for (i = 0; i < FIMC_MAX_DEVS; i++) {
768 struct fimc_dev *dev = fmd->fimc[i];
769 if (dev == NULL)
770 continue;
771 v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
772 dev->vid_cap.ve.pipe = NULL;
773 fmd->fimc[i] = NULL;
774 }
775 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
776 struct fimc_lite *dev = fmd->fimc_lite[i];
777 if (dev == NULL)
778 continue;
779 v4l2_device_unregister_subdev(&dev->subdev);
780 dev->ve.pipe = NULL;
781 fmd->fimc_lite[i] = NULL;
782 }
783 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
784 if (fmd->csis[i].sd == NULL)
785 continue;
786 v4l2_device_unregister_subdev(fmd->csis[i].sd);
787 fmd->csis[i].sd = NULL;
788 }
789
790 if (fmd->fimc_is)
791 v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
792
793 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
794 }
795
796 /**
797 * __fimc_md_create_fimc_sink_links - create links to all FIMC entities
798 * @fmd: fimc media device
799 * @source: the source entity to create links to all fimc entities from
800 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
801 * @pad: the source entity pad index
802 * @link_mask: bitmask of the fimc devices for which link should be enabled
803 */
__fimc_md_create_fimc_sink_links(struct fimc_md * fmd,struct media_entity * source,struct v4l2_subdev * sensor,int pad,int link_mask)804 static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
805 struct media_entity *source,
806 struct v4l2_subdev *sensor,
807 int pad, int link_mask)
808 {
809 struct fimc_source_info *si = NULL;
810 struct media_entity *sink;
811 unsigned int flags = 0;
812 int i, ret = 0;
813
814 if (sensor) {
815 si = v4l2_get_subdev_hostdata(sensor);
816 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
817 if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
818 ret = 1;
819 }
820
821 for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
822 if (!fmd->fimc[i])
823 continue;
824 /*
825 * Some FIMC variants are not fitted with camera capture
826 * interface. Skip creating a link from sensor for those.
827 */
828 if (!fmd->fimc[i]->variant->has_cam_if)
829 continue;
830
831 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
832
833 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
834 ret = media_create_pad_link(source, pad, sink,
835 FIMC_SD_PAD_SINK_CAM, flags);
836 if (ret)
837 return ret;
838
839 /* Notify FIMC capture subdev entity */
840 ret = media_entity_call(sink, link_setup, &sink->pads[0],
841 &source->pads[pad], flags);
842 if (ret)
843 break;
844
845 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
846 source->name, flags ? '=' : '-', sink->name);
847 }
848
849 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
850 if (!fmd->fimc_lite[i])
851 continue;
852
853 sink = &fmd->fimc_lite[i]->subdev.entity;
854 ret = media_create_pad_link(source, pad, sink,
855 FLITE_SD_PAD_SINK, 0);
856 if (ret)
857 return ret;
858
859 /* Notify FIMC-LITE subdev entity */
860 ret = media_entity_call(sink, link_setup, &sink->pads[0],
861 &source->pads[pad], 0);
862 if (ret)
863 break;
864
865 v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
866 source->name, sink->name);
867 }
868 return 0;
869 }
870
871 /* Create links from FIMC-LITE source pads to other entities */
__fimc_md_create_flite_source_links(struct fimc_md * fmd)872 static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
873 {
874 struct media_entity *source, *sink;
875 int i, ret = 0;
876
877 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
878 struct fimc_lite *fimc = fmd->fimc_lite[i];
879
880 if (fimc == NULL)
881 continue;
882
883 source = &fimc->subdev.entity;
884 sink = &fimc->ve.vdev.entity;
885 /* FIMC-LITE's subdev and video node */
886 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
887 sink, 0, 0);
888 if (ret)
889 break;
890 /* Link from FIMC-LITE to IS-ISP subdev */
891 sink = &fmd->fimc_is->isp.subdev.entity;
892 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
893 sink, 0, 0);
894 if (ret)
895 break;
896 }
897
898 return ret;
899 }
900
901 /* Create FIMC-IS links */
__fimc_md_create_fimc_is_links(struct fimc_md * fmd)902 static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
903 {
904 struct fimc_isp *isp = &fmd->fimc_is->isp;
905 struct media_entity *source, *sink;
906 int i, ret;
907
908 source = &isp->subdev.entity;
909
910 for (i = 0; i < FIMC_MAX_DEVS; i++) {
911 if (fmd->fimc[i] == NULL)
912 continue;
913
914 /* Link from FIMC-IS-ISP subdev to FIMC */
915 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
916 ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
917 sink, FIMC_SD_PAD_SINK_FIFO, 0);
918 if (ret)
919 return ret;
920 }
921
922 /* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
923 sink = &isp->video_capture.ve.vdev.entity;
924
925 /* Skip this link if the fimc-is-isp video node driver isn't built-in */
926 if (sink->num_pads == 0)
927 return 0;
928
929 return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
930 sink, 0, 0);
931 }
932
933 /**
934 * fimc_md_create_links - create default links between registered entities
935 * @fmd: fimc media device
936 *
937 * Parallel interface sensor entities are connected directly to FIMC capture
938 * entities. The sensors using MIPI CSIS bus are connected through immutable
939 * link with CSI receiver entity specified by mux_id. Any registered CSIS
940 * entity has a link to each registered FIMC capture entity. Enabled links
941 * are created by default between each subsequent registered sensor and
942 * subsequent FIMC capture entity. The number of default active links is
943 * determined by the number of available sensors or FIMC entities,
944 * whichever is less.
945 */
fimc_md_create_links(struct fimc_md * fmd)946 static int fimc_md_create_links(struct fimc_md *fmd)
947 {
948 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
949 struct v4l2_subdev *sensor, *csis;
950 struct fimc_source_info *pdata;
951 struct media_entity *source, *sink;
952 int i, pad, fimc_id = 0, ret = 0;
953 u32 flags, link_mask = 0;
954
955 for (i = 0; i < fmd->num_sensors; i++) {
956 if (fmd->sensor[i].subdev == NULL)
957 continue;
958
959 sensor = fmd->sensor[i].subdev;
960 pdata = v4l2_get_subdev_hostdata(sensor);
961 if (!pdata)
962 continue;
963
964 source = NULL;
965
966 switch (pdata->sensor_bus_type) {
967 case FIMC_BUS_TYPE_MIPI_CSI2:
968 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
969 "Wrong CSI channel id: %d\n", pdata->mux_id))
970 return -EINVAL;
971
972 csis = fmd->csis[pdata->mux_id].sd;
973 if (WARN(csis == NULL,
974 "MIPI-CSI interface specified but s5p-csis module is not loaded!\n"))
975 return -EINVAL;
976
977 pad = sensor->entity.num_pads - 1;
978 ret = media_create_pad_link(&sensor->entity, pad,
979 &csis->entity, CSIS_PAD_SINK,
980 MEDIA_LNK_FL_IMMUTABLE |
981 MEDIA_LNK_FL_ENABLED);
982 if (ret)
983 return ret;
984
985 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
986 sensor->entity.name, csis->entity.name);
987
988 source = NULL;
989 csi_sensors[pdata->mux_id] = sensor;
990 break;
991
992 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
993 source = &sensor->entity;
994 pad = 0;
995 break;
996
997 default:
998 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
999 pdata->sensor_bus_type);
1000 return -EINVAL;
1001 }
1002 if (source == NULL)
1003 continue;
1004
1005 link_mask = 1 << fimc_id++;
1006 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1007 pad, link_mask);
1008 }
1009
1010 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
1011 if (fmd->csis[i].sd == NULL)
1012 continue;
1013
1014 source = &fmd->csis[i].sd->entity;
1015 pad = CSIS_PAD_SOURCE;
1016 sensor = csi_sensors[i];
1017
1018 link_mask = 1 << fimc_id++;
1019 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1020 pad, link_mask);
1021 }
1022
1023 /* Create immutable links between each FIMC's subdev and video node */
1024 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1025 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1026 if (!fmd->fimc[i])
1027 continue;
1028
1029 source = &fmd->fimc[i]->vid_cap.subdev.entity;
1030 sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
1031
1032 ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
1033 sink, 0, flags);
1034 if (ret)
1035 break;
1036 }
1037
1038 ret = __fimc_md_create_flite_source_links(fmd);
1039 if (ret < 0)
1040 return ret;
1041
1042 if (fmd->use_isp)
1043 ret = __fimc_md_create_fimc_is_links(fmd);
1044
1045 return ret;
1046 }
1047
1048 /*
1049 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1050 */
fimc_md_put_clocks(struct fimc_md * fmd)1051 static void fimc_md_put_clocks(struct fimc_md *fmd)
1052 {
1053 int i = FIMC_MAX_CAMCLKS;
1054
1055 while (--i >= 0) {
1056 if (IS_ERR(fmd->camclk[i].clock))
1057 continue;
1058 clk_put(fmd->camclk[i].clock);
1059 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1060 }
1061
1062 /* Writeback (PIXELASYNCMx) clocks */
1063 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1064 if (IS_ERR(fmd->wbclk[i]))
1065 continue;
1066 clk_put(fmd->wbclk[i]);
1067 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1068 }
1069 }
1070
fimc_md_get_clocks(struct fimc_md * fmd)1071 static int fimc_md_get_clocks(struct fimc_md *fmd)
1072 {
1073 struct device *dev = &fmd->pdev->dev;
1074 char clk_name[32];
1075 struct clk *clock;
1076 int i, ret = 0;
1077
1078 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1079 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1080
1081 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1082 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1083 clock = clk_get(dev, clk_name);
1084
1085 if (IS_ERR(clock)) {
1086 dev_err(dev, "Failed to get clock: %s\n", clk_name);
1087 ret = PTR_ERR(clock);
1088 break;
1089 }
1090 fmd->camclk[i].clock = clock;
1091 }
1092 if (ret)
1093 fimc_md_put_clocks(fmd);
1094
1095 if (!fmd->use_isp)
1096 return 0;
1097 /*
1098 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1099 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1100 */
1101 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1102
1103 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1104 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1105 clock = clk_get(dev, clk_name);
1106 if (IS_ERR(clock)) {
1107 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1108 clk_name);
1109 ret = PTR_ERR(clock);
1110 break;
1111 }
1112 fmd->wbclk[i] = clock;
1113 }
1114 if (ret)
1115 fimc_md_put_clocks(fmd);
1116
1117 return ret;
1118 }
1119
__fimc_md_modify_pipeline(struct media_entity * entity,bool enable)1120 static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1121 {
1122 struct exynos_video_entity *ve;
1123 struct fimc_pipeline *p;
1124 struct video_device *vdev;
1125 int ret;
1126
1127 vdev = media_entity_to_video_device(entity);
1128 if (vdev->entity.use_count == 0)
1129 return 0;
1130
1131 ve = vdev_to_exynos_video_entity(vdev);
1132 p = to_fimc_pipeline(ve->pipe);
1133 /*
1134 * Nothing to do if we are disabling the pipeline, some link
1135 * has been disconnected and p->subdevs array is cleared now.
1136 */
1137 if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1138 return 0;
1139
1140 if (enable)
1141 ret = __fimc_pipeline_open(ve->pipe, entity, true);
1142 else
1143 ret = __fimc_pipeline_close(ve->pipe);
1144
1145 if (ret == 0 && !enable)
1146 memset(p->subdevs, 0, sizeof(p->subdevs));
1147
1148 return ret;
1149 }
1150
1151 /* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */
__fimc_md_modify_pipelines(struct media_entity * entity,bool enable,struct media_graph * graph)1152 static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
1153 struct media_graph *graph)
1154 {
1155 struct media_entity *entity_err = entity;
1156 int ret;
1157
1158 /*
1159 * Walk current graph and call the pipeline open/close routine for each
1160 * opened video node that belongs to the graph of entities connected
1161 * through active links. This is needed as we cannot power on/off the
1162 * subdevs in random order.
1163 */
1164 media_graph_walk_start(graph, entity);
1165
1166 while ((entity = media_graph_walk_next(graph))) {
1167 if (!is_media_entity_v4l2_video_device(entity))
1168 continue;
1169
1170 ret = __fimc_md_modify_pipeline(entity, enable);
1171
1172 if (ret < 0)
1173 goto err;
1174 }
1175
1176 return 0;
1177
1178 err:
1179 media_graph_walk_start(graph, entity_err);
1180
1181 while ((entity_err = media_graph_walk_next(graph))) {
1182 if (!is_media_entity_v4l2_video_device(entity_err))
1183 continue;
1184
1185 __fimc_md_modify_pipeline(entity_err, !enable);
1186
1187 if (entity_err == entity)
1188 break;
1189 }
1190
1191 return ret;
1192 }
1193
fimc_md_link_notify(struct media_link * link,unsigned int flags,unsigned int notification)1194 static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1195 unsigned int notification)
1196 {
1197 struct media_graph *graph =
1198 &container_of(link->graph_obj.mdev, struct fimc_md,
1199 media_dev)->link_setup_graph;
1200 struct media_entity *sink = link->sink->entity;
1201 int ret = 0;
1202
1203 /* Before link disconnection */
1204 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1205 ret = media_graph_walk_init(graph,
1206 link->graph_obj.mdev);
1207 if (ret)
1208 return ret;
1209 if (!(flags & MEDIA_LNK_FL_ENABLED))
1210 ret = __fimc_md_modify_pipelines(sink, false, graph);
1211 #if 0
1212 else
1213 /* TODO: Link state change validation */
1214 #endif
1215 /* After link activation */
1216 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
1217 if (link->flags & MEDIA_LNK_FL_ENABLED)
1218 ret = __fimc_md_modify_pipelines(sink, true, graph);
1219 media_graph_walk_cleanup(graph);
1220 }
1221
1222 return ret ? -EPIPE : 0;
1223 }
1224
1225 static const struct media_device_ops fimc_md_ops = {
1226 .link_notify = fimc_md_link_notify,
1227 };
1228
subdev_conf_mode_show(struct device * dev,struct device_attribute * attr,char * buf)1229 static ssize_t subdev_conf_mode_show(struct device *dev,
1230 struct device_attribute *attr, char *buf)
1231 {
1232 struct fimc_md *fmd = dev_get_drvdata(dev);
1233
1234 if (fmd->user_subdev_api)
1235 return strscpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1236
1237 return strscpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1238 }
1239
subdev_conf_mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1240 static ssize_t subdev_conf_mode_store(struct device *dev,
1241 struct device_attribute *attr,
1242 const char *buf, size_t count)
1243 {
1244 struct fimc_md *fmd = dev_get_drvdata(dev);
1245 bool subdev_api;
1246 int i;
1247
1248 if (!strcmp(buf, "vid-dev\n"))
1249 subdev_api = false;
1250 else if (!strcmp(buf, "sub-dev\n"))
1251 subdev_api = true;
1252 else
1253 return count;
1254
1255 fmd->user_subdev_api = subdev_api;
1256 for (i = 0; i < FIMC_MAX_DEVS; i++)
1257 if (fmd->fimc[i])
1258 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1259 return count;
1260 }
1261 /*
1262 * This device attribute is to select video pipeline configuration method.
1263 * There are following valid values:
1264 * vid-dev - for V4L2 video node API only, subdevice will be configured
1265 * by the host driver.
1266 * sub-dev - for media controller API, subdevs must be configured in user
1267 * space before starting streaming.
1268 */
1269 static DEVICE_ATTR_RW(subdev_conf_mode);
1270
cam_clk_prepare(struct clk_hw * hw)1271 static int cam_clk_prepare(struct clk_hw *hw)
1272 {
1273 struct cam_clk *camclk = to_cam_clk(hw);
1274
1275 if (camclk->fmd->pmf == NULL)
1276 return -ENODEV;
1277
1278 return pm_runtime_resume_and_get(camclk->fmd->pmf);
1279 }
1280
cam_clk_unprepare(struct clk_hw * hw)1281 static void cam_clk_unprepare(struct clk_hw *hw)
1282 {
1283 struct cam_clk *camclk = to_cam_clk(hw);
1284
1285 if (camclk->fmd->pmf == NULL)
1286 return;
1287
1288 pm_runtime_put_sync(camclk->fmd->pmf);
1289 }
1290
1291 static const struct clk_ops cam_clk_ops = {
1292 .prepare = cam_clk_prepare,
1293 .unprepare = cam_clk_unprepare,
1294 };
1295
fimc_md_unregister_clk_provider(struct fimc_md * fmd)1296 static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
1297 {
1298 struct cam_clk_provider *cp = &fmd->clk_provider;
1299 unsigned int i;
1300
1301 if (cp->of_node)
1302 of_clk_del_provider(cp->of_node);
1303
1304 for (i = 0; i < cp->num_clocks; i++)
1305 clk_unregister(cp->clks[i]);
1306 }
1307
fimc_md_register_clk_provider(struct fimc_md * fmd)1308 static int fimc_md_register_clk_provider(struct fimc_md *fmd)
1309 {
1310 struct cam_clk_provider *cp = &fmd->clk_provider;
1311 struct device *dev = &fmd->pdev->dev;
1312 int i, ret;
1313
1314 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1315 struct cam_clk *camclk = &cp->camclk[i];
1316 struct clk_init_data init;
1317 const char *p_name;
1318
1319 ret = of_property_read_string_index(dev->of_node,
1320 "clock-output-names", i, &init.name);
1321 if (ret < 0)
1322 break;
1323
1324 p_name = __clk_get_name(fmd->camclk[i].clock);
1325
1326 /* It's safe since clk_register() will duplicate the string. */
1327 init.parent_names = &p_name;
1328 init.num_parents = 1;
1329 init.ops = &cam_clk_ops;
1330 init.flags = CLK_SET_RATE_PARENT;
1331 camclk->hw.init = &init;
1332 camclk->fmd = fmd;
1333
1334 cp->clks[i] = clk_register(NULL, &camclk->hw);
1335 if (IS_ERR(cp->clks[i])) {
1336 dev_err(dev, "failed to register clock: %s (%pe)\n",
1337 init.name, cp->clks[i]);
1338 ret = PTR_ERR(cp->clks[i]);
1339 goto err;
1340 }
1341 cp->num_clocks++;
1342 }
1343
1344 if (cp->num_clocks == 0) {
1345 dev_warn(dev, "clk provider not registered\n");
1346 return 0;
1347 }
1348
1349 cp->clk_data.clks = cp->clks;
1350 cp->clk_data.clk_num = cp->num_clocks;
1351 cp->of_node = dev->of_node;
1352 ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1353 &cp->clk_data);
1354 if (ret == 0)
1355 return 0;
1356 err:
1357 fimc_md_unregister_clk_provider(fmd);
1358 return ret;
1359 }
1360
subdev_notifier_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_connection * asd)1361 static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
1362 struct v4l2_subdev *subdev,
1363 struct v4l2_async_connection *asd)
1364 {
1365 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1366 struct fimc_sensor_info *si = NULL;
1367 int i;
1368
1369 /* Find platform data for this sensor subdev */
1370 for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1371 if (fmd->sensor[i].asd == asd)
1372 si = &fmd->sensor[i];
1373
1374 if (si == NULL)
1375 return -EINVAL;
1376
1377 v4l2_set_subdev_hostdata(subdev, &si->pdata);
1378
1379 if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
1380 subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
1381 else
1382 subdev->grp_id = GRP_ID_SENSOR;
1383
1384 si->subdev = subdev;
1385
1386 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
1387 subdev->name, fmd->num_sensors);
1388
1389 fmd->num_sensors++;
1390
1391 return 0;
1392 }
1393
subdev_notifier_complete(struct v4l2_async_notifier * notifier)1394 static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
1395 {
1396 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1397 int ret;
1398
1399 mutex_lock(&fmd->media_dev.graph_mutex);
1400
1401 ret = fimc_md_create_links(fmd);
1402 if (ret < 0) {
1403 mutex_unlock(&fmd->media_dev.graph_mutex);
1404 return ret;
1405 }
1406
1407 mutex_unlock(&fmd->media_dev.graph_mutex);
1408
1409 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1410 if (ret < 0)
1411 return ret;
1412
1413 return media_device_register(&fmd->media_dev);
1414 }
1415
1416 static const struct v4l2_async_notifier_operations subdev_notifier_ops = {
1417 .bound = subdev_notifier_bound,
1418 .complete = subdev_notifier_complete,
1419 };
1420
fimc_md_probe(struct platform_device * pdev)1421 static int fimc_md_probe(struct platform_device *pdev)
1422 {
1423 struct device *dev = &pdev->dev;
1424 struct v4l2_device *v4l2_dev;
1425 struct pinctrl *pinctrl;
1426 struct fimc_md *fmd;
1427 int ret;
1428
1429 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1430 if (!fmd)
1431 return -ENOMEM;
1432
1433 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
1434 if (ret < 0)
1435 return -ENOMEM;
1436
1437 spin_lock_init(&fmd->slock);
1438 INIT_LIST_HEAD(&fmd->pipelines);
1439 fmd->pdev = pdev;
1440
1441 strscpy(fmd->media_dev.model, "Samsung S5P FIMC",
1442 sizeof(fmd->media_dev.model));
1443 fmd->media_dev.ops = &fimc_md_ops;
1444 fmd->media_dev.dev = dev;
1445
1446 v4l2_dev = &fmd->v4l2_dev;
1447 v4l2_dev->mdev = &fmd->media_dev;
1448 v4l2_dev->notify = fimc_sensor_notify;
1449 strscpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1450
1451 fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1452 fmd->user_subdev_api = true;
1453
1454 media_device_init(&fmd->media_dev);
1455
1456 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1457 if (ret < 0) {
1458 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1459 goto err_md;
1460 }
1461
1462 ret = fimc_md_get_clocks(fmd);
1463 if (ret)
1464 goto err_v4l2dev;
1465
1466 pinctrl = devm_pinctrl_get(dev);
1467 if (IS_ERR(pinctrl))
1468 dev_dbg(dev, "Failed to get pinctrl: %pe\n", pinctrl);
1469
1470 platform_set_drvdata(pdev, fmd);
1471
1472 v4l2_async_nf_init(&fmd->subdev_notifier, &fmd->v4l2_dev);
1473
1474 ret = fimc_md_register_platform_entities(fmd, dev->of_node);
1475 if (ret)
1476 goto err_clk;
1477
1478 ret = fimc_md_register_sensor_entities(fmd);
1479 if (ret)
1480 goto err_m_ent;
1481
1482 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1483 if (ret)
1484 goto err_cleanup;
1485 /*
1486 * FIMC platform devices need to be registered before the sclk_cam
1487 * clocks provider, as one of these devices needs to be activated
1488 * to enable the clock.
1489 */
1490 ret = fimc_md_register_clk_provider(fmd);
1491 if (ret < 0) {
1492 v4l2_err(v4l2_dev, "clock provider registration failed\n");
1493 goto err_attr;
1494 }
1495
1496 if (fmd->num_sensors > 0) {
1497 fmd->subdev_notifier.ops = &subdev_notifier_ops;
1498 fmd->num_sensors = 0;
1499
1500 ret = v4l2_async_nf_register(&fmd->subdev_notifier);
1501 if (ret)
1502 goto err_clk_p;
1503 }
1504
1505 return 0;
1506
1507 err_clk_p:
1508 fimc_md_unregister_clk_provider(fmd);
1509 err_attr:
1510 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1511 err_cleanup:
1512 v4l2_async_nf_cleanup(&fmd->subdev_notifier);
1513 err_m_ent:
1514 fimc_md_unregister_entities(fmd);
1515 err_clk:
1516 fimc_md_put_clocks(fmd);
1517 err_v4l2dev:
1518 v4l2_device_unregister(&fmd->v4l2_dev);
1519 err_md:
1520 media_device_cleanup(&fmd->media_dev);
1521 return ret;
1522 }
1523
fimc_md_remove(struct platform_device * pdev)1524 static void fimc_md_remove(struct platform_device *pdev)
1525 {
1526 struct fimc_md *fmd = platform_get_drvdata(pdev);
1527
1528 if (!fmd)
1529 return;
1530
1531 fimc_md_unregister_clk_provider(fmd);
1532 v4l2_async_nf_unregister(&fmd->subdev_notifier);
1533 v4l2_async_nf_cleanup(&fmd->subdev_notifier);
1534
1535 v4l2_device_unregister(&fmd->v4l2_dev);
1536 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1537 fimc_md_unregister_entities(fmd);
1538 fimc_md_pipelines_free(fmd);
1539 media_device_unregister(&fmd->media_dev);
1540 media_device_cleanup(&fmd->media_dev);
1541 fimc_md_put_clocks(fmd);
1542 }
1543
1544 static const struct platform_device_id fimc_driver_ids[] __always_unused = {
1545 { .name = "s5p-fimc-md" },
1546 { },
1547 };
1548 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1549
1550 static const struct of_device_id fimc_md_of_match[] = {
1551 { .compatible = "samsung,fimc" },
1552 { },
1553 };
1554 MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1555
1556 static struct platform_driver fimc_md_driver = {
1557 .probe = fimc_md_probe,
1558 .remove = fimc_md_remove,
1559 .driver = {
1560 .of_match_table = of_match_ptr(fimc_md_of_match),
1561 .name = "s5p-fimc-md",
1562 }
1563 };
1564
fimc_md_init(void)1565 static int __init fimc_md_init(void)
1566 {
1567 int ret;
1568
1569 request_module("s5p-csis");
1570 ret = fimc_register_driver();
1571 if (ret)
1572 return ret;
1573
1574 ret = platform_driver_register(&fimc_md_driver);
1575 if (ret)
1576 fimc_unregister_driver();
1577
1578 return ret;
1579 }
1580
fimc_md_exit(void)1581 static void __exit fimc_md_exit(void)
1582 {
1583 platform_driver_unregister(&fimc_md_driver);
1584 fimc_unregister_driver();
1585 }
1586
1587 module_init(fimc_md_init);
1588 module_exit(fimc_md_exit);
1589
1590 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1591 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1592 MODULE_LICENSE("GPL");
1593 MODULE_VERSION("2.0.1");
1594