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