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