Merge tag 'mmc-v4.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc
[cascardo/linux.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/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_of_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_of_parse_endpoint(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                 return -EINVAL;
407
408         pd->mux_id = (endpoint.base.port - 1) & 0x1;
409
410         rem = of_graph_get_remote_port_parent(ep);
411         of_node_put(ep);
412         if (rem == NULL) {
413                 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
414                                                         ep->full_name);
415                 return 0;
416         }
417
418         if (fimc_input_is_parallel(endpoint.base.port)) {
419                 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
420                         pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
421                 else
422                         pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
423                 pd->flags = endpoint.bus.parallel.flags;
424         } else if (fimc_input_is_mipi_csi(endpoint.base.port)) {
425                 /*
426                  * MIPI CSI-2: only input mux selection and
427                  * the sensor's clock frequency is needed.
428                  */
429                 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
430         } else {
431                 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
432                          endpoint.base.port, rem->full_name);
433         }
434         /*
435          * For FIMC-IS handled sensors, that are placed under i2c-isp device
436          * node, FIMC is connected to the FIMC-IS through its ISP Writeback
437          * input. Sensors are attached to the FIMC-LITE hostdata interface
438          * directly or through MIPI-CSIS, depending on the external media bus
439          * used. This needs to be handled in a more reliable way, not by just
440          * checking parent's node name.
441          */
442         np = of_get_parent(rem);
443
444         if (np && !of_node_cmp(np->name, "i2c-isp"))
445                 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
446         else
447                 pd->fimc_bus_type = pd->sensor_bus_type;
448
449         if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor))) {
450                 of_node_put(rem);
451                 return -EINVAL;
452         }
453
454         fmd->sensor[index].asd.match_type = V4L2_ASYNC_MATCH_OF;
455         fmd->sensor[index].asd.match.of.node = rem;
456         fmd->async_subdevs[index] = &fmd->sensor[index].asd;
457
458         fmd->num_sensors++;
459
460         of_node_put(rem);
461         return 0;
462 }
463
464 /* Register all SoC external sub-devices */
465 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
466 {
467         struct device_node *parent = fmd->pdev->dev.of_node;
468         struct device_node *node, *ports;
469         int index = 0;
470         int ret;
471
472         /*
473          * Runtime resume one of the FIMC entities to make sure
474          * the sclk_cam clocks are not globally disabled.
475          */
476         if (!fmd->pmf)
477                 return -ENXIO;
478
479         ret = pm_runtime_get_sync(fmd->pmf);
480         if (ret < 0)
481                 return ret;
482
483         fmd->num_sensors = 0;
484
485         /* Attach sensors linked to MIPI CSI-2 receivers */
486         for_each_available_child_of_node(parent, node) {
487                 struct device_node *port;
488
489                 if (of_node_cmp(node->name, "csis"))
490                         continue;
491                 /* The csis node can have only port subnode. */
492                 port = of_get_next_child(node, NULL);
493                 if (!port)
494                         continue;
495
496                 ret = fimc_md_parse_port_node(fmd, port, index);
497                 if (ret < 0) {
498                         of_node_put(node);
499                         goto rpm_put;
500                 }
501                 index++;
502         }
503
504         /* Attach sensors listed in the parallel-ports node */
505         ports = of_get_child_by_name(parent, "parallel-ports");
506         if (!ports)
507                 goto rpm_put;
508
509         for_each_child_of_node(ports, node) {
510                 ret = fimc_md_parse_port_node(fmd, node, index);
511                 if (ret < 0) {
512                         of_node_put(node);
513                         break;
514                 }
515                 index++;
516         }
517 rpm_put:
518         pm_runtime_put(fmd->pmf);
519         return ret;
520 }
521
522 static int __of_get_csis_id(struct device_node *np)
523 {
524         u32 reg = 0;
525
526         np = of_get_child_by_name(np, "port");
527         if (!np)
528                 return -EINVAL;
529         of_property_read_u32(np, "reg", &reg);
530         return reg - FIMC_INPUT_MIPI_CSI2_0;
531 }
532
533 /*
534  * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
535  */
536 static int register_fimc_lite_entity(struct fimc_md *fmd,
537                                      struct fimc_lite *fimc_lite)
538 {
539         struct v4l2_subdev *sd;
540         struct exynos_media_pipeline *ep;
541         int ret;
542
543         if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
544                     fmd->fimc_lite[fimc_lite->index]))
545                 return -EBUSY;
546
547         sd = &fimc_lite->subdev;
548         sd->grp_id = GRP_ID_FLITE;
549
550         ep = fimc_md_pipeline_create(fmd);
551         if (!ep)
552                 return -ENOMEM;
553
554         v4l2_set_subdev_hostdata(sd, ep);
555
556         ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
557         if (!ret)
558                 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
559         else
560                 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
561                          fimc_lite->index);
562         return ret;
563 }
564
565 static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
566 {
567         struct v4l2_subdev *sd;
568         struct exynos_media_pipeline *ep;
569         int ret;
570
571         if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
572                 return -EBUSY;
573
574         sd = &fimc->vid_cap.subdev;
575         sd->grp_id = GRP_ID_FIMC;
576
577         ep = fimc_md_pipeline_create(fmd);
578         if (!ep)
579                 return -ENOMEM;
580
581         v4l2_set_subdev_hostdata(sd, ep);
582
583         ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
584         if (!ret) {
585                 if (!fmd->pmf && fimc->pdev)
586                         fmd->pmf = &fimc->pdev->dev;
587                 fmd->fimc[fimc->id] = fimc;
588                 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
589         } else {
590                 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
591                          fimc->id, ret);
592         }
593         return ret;
594 }
595
596 static int register_csis_entity(struct fimc_md *fmd,
597                                 struct platform_device *pdev,
598                                 struct v4l2_subdev *sd)
599 {
600         struct device_node *node = pdev->dev.of_node;
601         int id, ret;
602
603         id = node ? __of_get_csis_id(node) : max(0, pdev->id);
604
605         if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
606                 return -ENOENT;
607
608         if (WARN_ON(fmd->csis[id].sd))
609                 return -EBUSY;
610
611         sd->grp_id = GRP_ID_CSIS;
612         ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
613         if (!ret)
614                 fmd->csis[id].sd = sd;
615         else
616                 v4l2_err(&fmd->v4l2_dev,
617                          "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
618         return ret;
619 }
620
621 static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
622 {
623         struct v4l2_subdev *sd = &is->isp.subdev;
624         struct exynos_media_pipeline *ep;
625         int ret;
626
627         /* Allocate pipeline object for the ISP capture video node. */
628         ep = fimc_md_pipeline_create(fmd);
629         if (!ep)
630                 return -ENOMEM;
631
632         v4l2_set_subdev_hostdata(sd, ep);
633
634         ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
635         if (ret) {
636                 v4l2_err(&fmd->v4l2_dev,
637                          "Failed to register FIMC-ISP (%d)\n", ret);
638                 return ret;
639         }
640
641         fmd->fimc_is = is;
642         return 0;
643 }
644
645 static int fimc_md_register_platform_entity(struct fimc_md *fmd,
646                                             struct platform_device *pdev,
647                                             int plat_entity)
648 {
649         struct device *dev = &pdev->dev;
650         int ret = -EPROBE_DEFER;
651         void *drvdata;
652
653         /* Lock to ensure dev->driver won't change. */
654         device_lock(dev);
655
656         if (!dev->driver || !try_module_get(dev->driver->owner))
657                 goto dev_unlock;
658
659         drvdata = dev_get_drvdata(dev);
660         /* Some subdev didn't probe successfully id drvdata is NULL */
661         if (drvdata) {
662                 switch (plat_entity) {
663                 case IDX_FIMC:
664                         ret = register_fimc_entity(fmd, drvdata);
665                         break;
666                 case IDX_FLITE:
667                         ret = register_fimc_lite_entity(fmd, drvdata);
668                         break;
669                 case IDX_CSIS:
670                         ret = register_csis_entity(fmd, pdev, drvdata);
671                         break;
672                 case IDX_IS_ISP:
673                         ret = register_fimc_is_entity(fmd, drvdata);
674                         break;
675                 default:
676                         ret = -ENODEV;
677                 }
678         }
679
680         module_put(dev->driver->owner);
681 dev_unlock:
682         device_unlock(dev);
683         if (ret == -EPROBE_DEFER)
684                 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
685                         dev_name(dev));
686         else if (ret < 0)
687                 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
688                         dev_name(dev), ret);
689         return ret;
690 }
691
692 /* Register FIMC, FIMC-LITE and CSIS media entities */
693 static int fimc_md_register_platform_entities(struct fimc_md *fmd,
694                                               struct device_node *parent)
695 {
696         struct device_node *node;
697         int ret = 0;
698
699         for_each_available_child_of_node(parent, node) {
700                 struct platform_device *pdev;
701                 int plat_entity = -1;
702
703                 pdev = of_find_device_by_node(node);
704                 if (!pdev)
705                         continue;
706
707                 /* If driver of any entity isn't ready try all again later. */
708                 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
709                         plat_entity = IDX_CSIS;
710                 else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
711                         plat_entity = IDX_IS_ISP;
712                 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
713                         plat_entity = IDX_FLITE;
714                 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
715                          !of_property_read_bool(node, "samsung,lcd-wb"))
716                         plat_entity = IDX_FIMC;
717
718                 if (plat_entity >= 0)
719                         ret = fimc_md_register_platform_entity(fmd, pdev,
720                                                         plat_entity);
721                 put_device(&pdev->dev);
722                 if (ret < 0) {
723                         of_node_put(node);
724                         break;
725                 }
726         }
727
728         return ret;
729 }
730
731 static void fimc_md_unregister_entities(struct fimc_md *fmd)
732 {
733         int i;
734
735         for (i = 0; i < FIMC_MAX_DEVS; i++) {
736                 struct fimc_dev *dev = fmd->fimc[i];
737                 if (dev == NULL)
738                         continue;
739                 v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
740                 dev->vid_cap.ve.pipe = NULL;
741                 fmd->fimc[i] = NULL;
742         }
743         for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
744                 struct fimc_lite *dev = fmd->fimc_lite[i];
745                 if (dev == NULL)
746                         continue;
747                 v4l2_device_unregister_subdev(&dev->subdev);
748                 dev->ve.pipe = NULL;
749                 fmd->fimc_lite[i] = NULL;
750         }
751         for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
752                 if (fmd->csis[i].sd == NULL)
753                         continue;
754                 v4l2_device_unregister_subdev(fmd->csis[i].sd);
755                 fmd->csis[i].sd = NULL;
756         }
757
758         if (fmd->fimc_is)
759                 v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
760
761         v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
762 }
763
764 /**
765  * __fimc_md_create_fimc_links - create links to all FIMC entities
766  * @fmd: fimc media device
767  * @source: the source entity to create links to all fimc entities from
768  * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
769  * @pad: the source entity pad index
770  * @link_mask: bitmask of the fimc devices for which link should be enabled
771  */
772 static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
773                                             struct media_entity *source,
774                                             struct v4l2_subdev *sensor,
775                                             int pad, int link_mask)
776 {
777         struct fimc_source_info *si = NULL;
778         struct media_entity *sink;
779         unsigned int flags = 0;
780         int i, ret = 0;
781
782         if (sensor) {
783                 si = v4l2_get_subdev_hostdata(sensor);
784                 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
785                 if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
786                         ret = 1;
787         }
788
789         for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
790                 if (!fmd->fimc[i])
791                         continue;
792                 /*
793                  * Some FIMC variants are not fitted with camera capture
794                  * interface. Skip creating a link from sensor for those.
795                  */
796                 if (!fmd->fimc[i]->variant->has_cam_if)
797                         continue;
798
799                 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
800
801                 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
802                 ret = media_create_pad_link(source, pad, sink,
803                                               FIMC_SD_PAD_SINK_CAM, flags);
804                 if (ret)
805                         return ret;
806
807                 /* Notify FIMC capture subdev entity */
808                 ret = media_entity_call(sink, link_setup, &sink->pads[0],
809                                         &source->pads[pad], flags);
810                 if (ret)
811                         break;
812
813                 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
814                           source->name, flags ? '=' : '-', sink->name);
815         }
816
817         for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
818                 if (!fmd->fimc_lite[i])
819                         continue;
820
821                 sink = &fmd->fimc_lite[i]->subdev.entity;
822                 ret = media_create_pad_link(source, pad, sink,
823                                                FLITE_SD_PAD_SINK, 0);
824                 if (ret)
825                         return ret;
826
827                 /* Notify FIMC-LITE subdev entity */
828                 ret = media_entity_call(sink, link_setup, &sink->pads[0],
829                                         &source->pads[pad], 0);
830                 if (ret)
831                         break;
832
833                 v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
834                           source->name, sink->name);
835         }
836         return 0;
837 }
838
839 /* Create links from FIMC-LITE source pads to other entities */
840 static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
841 {
842         struct media_entity *source, *sink;
843         int i, ret = 0;
844
845         for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
846                 struct fimc_lite *fimc = fmd->fimc_lite[i];
847
848                 if (fimc == NULL)
849                         continue;
850
851                 source = &fimc->subdev.entity;
852                 sink = &fimc->ve.vdev.entity;
853                 /* FIMC-LITE's subdev and video node */
854                 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
855                                                sink, 0, 0);
856                 if (ret)
857                         break;
858                 /* Link from FIMC-LITE to IS-ISP subdev */
859                 sink = &fmd->fimc_is->isp.subdev.entity;
860                 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
861                                                sink, 0, 0);
862                 if (ret)
863                         break;
864         }
865
866         return ret;
867 }
868
869 /* Create FIMC-IS links */
870 static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
871 {
872         struct fimc_isp *isp = &fmd->fimc_is->isp;
873         struct media_entity *source, *sink;
874         int i, ret;
875
876         source = &isp->subdev.entity;
877
878         for (i = 0; i < FIMC_MAX_DEVS; i++) {
879                 if (fmd->fimc[i] == NULL)
880                         continue;
881
882                 /* Link from FIMC-IS-ISP subdev to FIMC */
883                 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
884                 ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
885                                                sink, FIMC_SD_PAD_SINK_FIFO, 0);
886                 if (ret)
887                         return ret;
888         }
889
890         /* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
891         sink = &isp->video_capture.ve.vdev.entity;
892
893         /* Skip this link if the fimc-is-isp video node driver isn't built-in */
894         if (sink->num_pads == 0)
895                 return 0;
896
897         return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
898                                         sink, 0, 0);
899 }
900
901 /**
902  * fimc_md_create_links - create default links between registered entities
903  *
904  * Parallel interface sensor entities are connected directly to FIMC capture
905  * entities. The sensors using MIPI CSIS bus are connected through immutable
906  * link with CSI receiver entity specified by mux_id. Any registered CSIS
907  * entity has a link to each registered FIMC capture entity. Enabled links
908  * are created by default between each subsequent registered sensor and
909  * subsequent FIMC capture entity. The number of default active links is
910  * determined by the number of available sensors or FIMC entities,
911  * whichever is less.
912  */
913 static int fimc_md_create_links(struct fimc_md *fmd)
914 {
915         struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
916         struct v4l2_subdev *sensor, *csis;
917         struct fimc_source_info *pdata;
918         struct media_entity *source, *sink;
919         int i, pad, fimc_id = 0, ret = 0;
920         u32 flags, link_mask = 0;
921
922         for (i = 0; i < fmd->num_sensors; i++) {
923                 if (fmd->sensor[i].subdev == NULL)
924                         continue;
925
926                 sensor = fmd->sensor[i].subdev;
927                 pdata = v4l2_get_subdev_hostdata(sensor);
928                 if (!pdata)
929                         continue;
930
931                 source = NULL;
932
933                 switch (pdata->sensor_bus_type) {
934                 case FIMC_BUS_TYPE_MIPI_CSI2:
935                         if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
936                                 "Wrong CSI channel id: %d\n", pdata->mux_id))
937                                 return -EINVAL;
938
939                         csis = fmd->csis[pdata->mux_id].sd;
940                         if (WARN(csis == NULL,
941                                  "MIPI-CSI interface specified "
942                                  "but s5p-csis module is not loaded!\n"))
943                                 return -EINVAL;
944
945                         pad = sensor->entity.num_pads - 1;
946                         ret = media_create_pad_link(&sensor->entity, pad,
947                                               &csis->entity, CSIS_PAD_SINK,
948                                               MEDIA_LNK_FL_IMMUTABLE |
949                                               MEDIA_LNK_FL_ENABLED);
950                         if (ret)
951                                 return ret;
952
953                         v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
954                                   sensor->entity.name, csis->entity.name);
955
956                         source = NULL;
957                         csi_sensors[pdata->mux_id] = sensor;
958                         break;
959
960                 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
961                         source = &sensor->entity;
962                         pad = 0;
963                         break;
964
965                 default:
966                         v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
967                                  pdata->sensor_bus_type);
968                         return -EINVAL;
969                 }
970                 if (source == NULL)
971                         continue;
972
973                 link_mask = 1 << fimc_id++;
974                 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
975                                                        pad, link_mask);
976         }
977
978         for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
979                 if (fmd->csis[i].sd == NULL)
980                         continue;
981
982                 source = &fmd->csis[i].sd->entity;
983                 pad = CSIS_PAD_SOURCE;
984                 sensor = csi_sensors[i];
985
986                 link_mask = 1 << fimc_id++;
987                 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
988                                                        pad, link_mask);
989         }
990
991         /* Create immutable links between each FIMC's subdev and video node */
992         flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
993         for (i = 0; i < FIMC_MAX_DEVS; i++) {
994                 if (!fmd->fimc[i])
995                         continue;
996
997                 source = &fmd->fimc[i]->vid_cap.subdev.entity;
998                 sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
999
1000                 ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
1001                                               sink, 0, flags);
1002                 if (ret)
1003                         break;
1004         }
1005
1006         ret = __fimc_md_create_flite_source_links(fmd);
1007         if (ret < 0)
1008                 return ret;
1009
1010         if (fmd->use_isp)
1011                 ret = __fimc_md_create_fimc_is_links(fmd);
1012
1013         return ret;
1014 }
1015
1016 /*
1017  * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1018  */
1019 static void fimc_md_put_clocks(struct fimc_md *fmd)
1020 {
1021         int i = FIMC_MAX_CAMCLKS;
1022
1023         while (--i >= 0) {
1024                 if (IS_ERR(fmd->camclk[i].clock))
1025                         continue;
1026                 clk_put(fmd->camclk[i].clock);
1027                 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1028         }
1029
1030         /* Writeback (PIXELASYNCMx) clocks */
1031         for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1032                 if (IS_ERR(fmd->wbclk[i]))
1033                         continue;
1034                 clk_put(fmd->wbclk[i]);
1035                 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1036         }
1037 }
1038
1039 static int fimc_md_get_clocks(struct fimc_md *fmd)
1040 {
1041         struct device *dev = &fmd->pdev->dev;
1042         char clk_name[32];
1043         struct clk *clock;
1044         int i, ret = 0;
1045
1046         for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1047                 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1048
1049         for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1050                 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1051                 clock = clk_get(dev, clk_name);
1052
1053                 if (IS_ERR(clock)) {
1054                         dev_err(dev, "Failed to get clock: %s\n", clk_name);
1055                         ret = PTR_ERR(clock);
1056                         break;
1057                 }
1058                 fmd->camclk[i].clock = clock;
1059         }
1060         if (ret)
1061                 fimc_md_put_clocks(fmd);
1062
1063         if (!fmd->use_isp)
1064                 return 0;
1065         /*
1066          * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1067          * leave PIXELASYNCM0 out for the LCD Writeback driver.
1068          */
1069         fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1070
1071         for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1072                 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1073                 clock = clk_get(dev, clk_name);
1074                 if (IS_ERR(clock)) {
1075                         v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1076                                   clk_name);
1077                         ret = PTR_ERR(clock);
1078                         break;
1079                 }
1080                 fmd->wbclk[i] = clock;
1081         }
1082         if (ret)
1083                 fimc_md_put_clocks(fmd);
1084
1085         return ret;
1086 }
1087
1088 static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1089 {
1090         struct exynos_video_entity *ve;
1091         struct fimc_pipeline *p;
1092         struct video_device *vdev;
1093         int ret;
1094
1095         vdev = media_entity_to_video_device(entity);
1096         if (vdev->entity.use_count == 0)
1097                 return 0;
1098
1099         ve = vdev_to_exynos_video_entity(vdev);
1100         p = to_fimc_pipeline(ve->pipe);
1101         /*
1102          * Nothing to do if we are disabling the pipeline, some link
1103          * has been disconnected and p->subdevs array is cleared now.
1104          */
1105         if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1106                 return 0;
1107
1108         if (enable)
1109                 ret = __fimc_pipeline_open(ve->pipe, entity, true);
1110         else
1111                 ret = __fimc_pipeline_close(ve->pipe);
1112
1113         if (ret == 0 && !enable)
1114                 memset(p->subdevs, 0, sizeof(p->subdevs));
1115
1116         return ret;
1117 }
1118
1119 /* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */
1120 static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
1121                                       struct media_entity_graph *graph)
1122 {
1123         struct media_entity *entity_err = entity;
1124         int ret;
1125
1126         /*
1127          * Walk current graph and call the pipeline open/close routine for each
1128          * opened video node that belongs to the graph of entities connected
1129          * through active links. This is needed as we cannot power on/off the
1130          * subdevs in random order.
1131          */
1132         media_entity_graph_walk_start(graph, entity);
1133
1134         while ((entity = media_entity_graph_walk_next(graph))) {
1135                 if (!is_media_entity_v4l2_video_device(entity))
1136                         continue;
1137
1138                 ret  = __fimc_md_modify_pipeline(entity, enable);
1139
1140                 if (ret < 0)
1141                         goto err;
1142         }
1143
1144         return 0;
1145
1146 err:
1147         media_entity_graph_walk_start(graph, entity_err);
1148
1149         while ((entity_err = media_entity_graph_walk_next(graph))) {
1150                 if (!is_media_entity_v4l2_video_device(entity_err))
1151                         continue;
1152
1153                 __fimc_md_modify_pipeline(entity_err, !enable);
1154
1155                 if (entity_err == entity)
1156                         break;
1157         }
1158
1159         return ret;
1160 }
1161
1162 static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1163                                 unsigned int notification)
1164 {
1165         struct media_entity_graph *graph =
1166                 &container_of(link->graph_obj.mdev, struct fimc_md,
1167                               media_dev)->link_setup_graph;
1168         struct media_entity *sink = link->sink->entity;
1169         int ret = 0;
1170
1171         /* Before link disconnection */
1172         if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1173                 ret = media_entity_graph_walk_init(graph,
1174                                                    link->graph_obj.mdev);
1175                 if (ret)
1176                         return ret;
1177                 if (!(flags & MEDIA_LNK_FL_ENABLED))
1178                         ret = __fimc_md_modify_pipelines(sink, false, graph);
1179 #if 0
1180                 else
1181                         /* TODO: Link state change validation */
1182 #endif
1183         /* After link activation */
1184         } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
1185                 if (link->flags & MEDIA_LNK_FL_ENABLED)
1186                         ret = __fimc_md_modify_pipelines(sink, true, graph);
1187                 media_entity_graph_walk_cleanup(graph);
1188         }
1189
1190         return ret ? -EPIPE : 0;
1191 }
1192
1193 static const struct media_device_ops fimc_md_ops = {
1194         .link_notify = fimc_md_link_notify,
1195 };
1196
1197 static ssize_t fimc_md_sysfs_show(struct device *dev,
1198                                   struct device_attribute *attr, char *buf)
1199 {
1200         struct platform_device *pdev = to_platform_device(dev);
1201         struct fimc_md *fmd = platform_get_drvdata(pdev);
1202
1203         if (fmd->user_subdev_api)
1204                 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1205
1206         return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1207 }
1208
1209 static ssize_t fimc_md_sysfs_store(struct device *dev,
1210                                    struct device_attribute *attr,
1211                                    const char *buf, size_t count)
1212 {
1213         struct platform_device *pdev = to_platform_device(dev);
1214         struct fimc_md *fmd = platform_get_drvdata(pdev);
1215         bool subdev_api;
1216         int i;
1217
1218         if (!strcmp(buf, "vid-dev\n"))
1219                 subdev_api = false;
1220         else if (!strcmp(buf, "sub-dev\n"))
1221                 subdev_api = true;
1222         else
1223                 return count;
1224
1225         fmd->user_subdev_api = subdev_api;
1226         for (i = 0; i < FIMC_MAX_DEVS; i++)
1227                 if (fmd->fimc[i])
1228                         fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1229         return count;
1230 }
1231 /*
1232  * This device attribute is to select video pipeline configuration method.
1233  * There are following valid values:
1234  *  vid-dev - for V4L2 video node API only, subdevice will be configured
1235  *  by the host driver.
1236  *  sub-dev - for media controller API, subdevs must be configured in user
1237  *  space before starting streaming.
1238  */
1239 static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1240                    fimc_md_sysfs_show, fimc_md_sysfs_store);
1241
1242 static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1243 {
1244         struct device *dev = &fmd->pdev->dev;
1245         struct fimc_pinctrl *pctl = &fmd->pinctl;
1246
1247         pctl->pinctrl = devm_pinctrl_get(dev);
1248         if (IS_ERR(pctl->pinctrl))
1249                 return PTR_ERR(pctl->pinctrl);
1250
1251         pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1252                                         PINCTRL_STATE_DEFAULT);
1253         if (IS_ERR(pctl->state_default))
1254                 return PTR_ERR(pctl->state_default);
1255
1256         pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1257                                         PINCTRL_STATE_IDLE);
1258         return 0;
1259 }
1260
1261 static int cam_clk_prepare(struct clk_hw *hw)
1262 {
1263         struct cam_clk *camclk = to_cam_clk(hw);
1264         int ret;
1265
1266         if (camclk->fmd->pmf == NULL)
1267                 return -ENODEV;
1268
1269         ret = pm_runtime_get_sync(camclk->fmd->pmf);
1270         return ret < 0 ? ret : 0;
1271 }
1272
1273 static void cam_clk_unprepare(struct clk_hw *hw)
1274 {
1275         struct cam_clk *camclk = to_cam_clk(hw);
1276
1277         if (camclk->fmd->pmf == NULL)
1278                 return;
1279
1280         pm_runtime_put_sync(camclk->fmd->pmf);
1281 }
1282
1283 static const struct clk_ops cam_clk_ops = {
1284         .prepare = cam_clk_prepare,
1285         .unprepare = cam_clk_unprepare,
1286 };
1287
1288 static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
1289 {
1290         struct cam_clk_provider *cp = &fmd->clk_provider;
1291         unsigned int i;
1292
1293         if (cp->of_node)
1294                 of_clk_del_provider(cp->of_node);
1295
1296         for (i = 0; i < cp->num_clocks; i++)
1297                 clk_unregister(cp->clks[i]);
1298 }
1299
1300 static int fimc_md_register_clk_provider(struct fimc_md *fmd)
1301 {
1302         struct cam_clk_provider *cp = &fmd->clk_provider;
1303         struct device *dev = &fmd->pdev->dev;
1304         int i, ret;
1305
1306         for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1307                 struct cam_clk *camclk = &cp->camclk[i];
1308                 struct clk_init_data init;
1309                 const char *p_name;
1310
1311                 ret = of_property_read_string_index(dev->of_node,
1312                                         "clock-output-names", i, &init.name);
1313                 if (ret < 0)
1314                         break;
1315
1316                 p_name = __clk_get_name(fmd->camclk[i].clock);
1317
1318                 /* It's safe since clk_register() will duplicate the string. */
1319                 init.parent_names = &p_name;
1320                 init.num_parents = 1;
1321                 init.ops = &cam_clk_ops;
1322                 init.flags = CLK_SET_RATE_PARENT;
1323                 camclk->hw.init = &init;
1324                 camclk->fmd = fmd;
1325
1326                 cp->clks[i] = clk_register(NULL, &camclk->hw);
1327                 if (IS_ERR(cp->clks[i])) {
1328                         dev_err(dev, "failed to register clock: %s (%ld)\n",
1329                                         init.name, PTR_ERR(cp->clks[i]));
1330                         ret = PTR_ERR(cp->clks[i]);
1331                         goto err;
1332                 }
1333                 cp->num_clocks++;
1334         }
1335
1336         if (cp->num_clocks == 0) {
1337                 dev_warn(dev, "clk provider not registered\n");
1338                 return 0;
1339         }
1340
1341         cp->clk_data.clks = cp->clks;
1342         cp->clk_data.clk_num = cp->num_clocks;
1343         cp->of_node = dev->of_node;
1344         ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1345                                   &cp->clk_data);
1346         if (ret == 0)
1347                 return 0;
1348 err:
1349         fimc_md_unregister_clk_provider(fmd);
1350         return ret;
1351 }
1352
1353 static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
1354                                  struct v4l2_subdev *subdev,
1355                                  struct v4l2_async_subdev *asd)
1356 {
1357         struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1358         struct fimc_sensor_info *si = NULL;
1359         int i;
1360
1361         /* Find platform data for this sensor subdev */
1362         for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1363                 if (fmd->sensor[i].asd.match.of.node == subdev->dev->of_node)
1364                         si = &fmd->sensor[i];
1365
1366         if (si == NULL)
1367                 return -EINVAL;
1368
1369         v4l2_set_subdev_hostdata(subdev, &si->pdata);
1370
1371         if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
1372                 subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
1373         else
1374                 subdev->grp_id = GRP_ID_SENSOR;
1375
1376         si->subdev = subdev;
1377
1378         v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
1379                   subdev->name, fmd->num_sensors);
1380
1381         fmd->num_sensors++;
1382
1383         return 0;
1384 }
1385
1386 static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
1387 {
1388         struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1389         int ret;
1390
1391         mutex_lock(&fmd->media_dev.graph_mutex);
1392
1393         ret = fimc_md_create_links(fmd);
1394         if (ret < 0)
1395                 goto unlock;
1396
1397         ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1398 unlock:
1399         mutex_unlock(&fmd->media_dev.graph_mutex);
1400         if (ret < 0)
1401                 return ret;
1402
1403         return media_device_register(&fmd->media_dev);
1404 }
1405
1406 static int fimc_md_probe(struct platform_device *pdev)
1407 {
1408         struct device *dev = &pdev->dev;
1409         struct v4l2_device *v4l2_dev;
1410         struct fimc_md *fmd;
1411         int ret;
1412
1413         fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1414         if (!fmd)
1415                 return -ENOMEM;
1416
1417         spin_lock_init(&fmd->slock);
1418         INIT_LIST_HEAD(&fmd->pipelines);
1419         fmd->pdev = pdev;
1420
1421         strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1422                 sizeof(fmd->media_dev.model));
1423         fmd->media_dev.ops = &fimc_md_ops;
1424         fmd->media_dev.dev = dev;
1425
1426         v4l2_dev = &fmd->v4l2_dev;
1427         v4l2_dev->mdev = &fmd->media_dev;
1428         v4l2_dev->notify = fimc_sensor_notify;
1429         strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1430
1431         fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1432         fmd->user_subdev_api = true;
1433
1434         media_device_init(&fmd->media_dev);
1435
1436         ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1437         if (ret < 0) {
1438                 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1439                 return ret;
1440         }
1441
1442         ret = fimc_md_get_clocks(fmd);
1443         if (ret)
1444                 goto err_md;
1445
1446         ret = fimc_md_get_pinctrl(fmd);
1447         if (ret < 0) {
1448                 if (ret != EPROBE_DEFER)
1449                         dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1450                 goto err_clk;
1451         }
1452
1453         platform_set_drvdata(pdev, fmd);
1454
1455         ret = fimc_md_register_platform_entities(fmd, dev->of_node);
1456         if (ret)
1457                 goto err_clk;
1458
1459         ret = fimc_md_register_sensor_entities(fmd);
1460         if (ret)
1461                 goto err_m_ent;
1462
1463         ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1464         if (ret)
1465                 goto err_m_ent;
1466         /*
1467          * FIMC platform devices need to be registered before the sclk_cam
1468          * clocks provider, as one of these devices needs to be activated
1469          * to enable the clock.
1470          */
1471         ret = fimc_md_register_clk_provider(fmd);
1472         if (ret < 0) {
1473                 v4l2_err(v4l2_dev, "clock provider registration failed\n");
1474                 goto err_attr;
1475         }
1476
1477         if (fmd->num_sensors > 0) {
1478                 fmd->subdev_notifier.subdevs = fmd->async_subdevs;
1479                 fmd->subdev_notifier.num_subdevs = fmd->num_sensors;
1480                 fmd->subdev_notifier.bound = subdev_notifier_bound;
1481                 fmd->subdev_notifier.complete = subdev_notifier_complete;
1482                 fmd->num_sensors = 0;
1483
1484                 ret = v4l2_async_notifier_register(&fmd->v4l2_dev,
1485                                                 &fmd->subdev_notifier);
1486                 if (ret)
1487                         goto err_clk_p;
1488         }
1489
1490         return 0;
1491
1492 err_clk_p:
1493         fimc_md_unregister_clk_provider(fmd);
1494 err_attr:
1495         device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1496 err_clk:
1497         fimc_md_put_clocks(fmd);
1498 err_m_ent:
1499         fimc_md_unregister_entities(fmd);
1500 err_md:
1501         media_device_cleanup(&fmd->media_dev);
1502         v4l2_device_unregister(&fmd->v4l2_dev);
1503         return ret;
1504 }
1505
1506 static int fimc_md_remove(struct platform_device *pdev)
1507 {
1508         struct fimc_md *fmd = platform_get_drvdata(pdev);
1509
1510         if (!fmd)
1511                 return 0;
1512
1513         fimc_md_unregister_clk_provider(fmd);
1514         v4l2_async_notifier_unregister(&fmd->subdev_notifier);
1515
1516         v4l2_device_unregister(&fmd->v4l2_dev);
1517         device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1518         fimc_md_unregister_entities(fmd);
1519         fimc_md_pipelines_free(fmd);
1520         media_device_unregister(&fmd->media_dev);
1521         media_device_cleanup(&fmd->media_dev);
1522         fimc_md_put_clocks(fmd);
1523
1524         return 0;
1525 }
1526
1527 static const struct platform_device_id fimc_driver_ids[] __always_unused = {
1528         { .name = "s5p-fimc-md" },
1529         { },
1530 };
1531 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1532
1533 static const struct of_device_id fimc_md_of_match[] = {
1534         { .compatible = "samsung,fimc" },
1535         { },
1536 };
1537 MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1538
1539 static struct platform_driver fimc_md_driver = {
1540         .probe          = fimc_md_probe,
1541         .remove         = fimc_md_remove,
1542         .driver = {
1543                 .of_match_table = of_match_ptr(fimc_md_of_match),
1544                 .name           = "s5p-fimc-md",
1545         }
1546 };
1547
1548 static int __init fimc_md_init(void)
1549 {
1550         int ret;
1551
1552         request_module("s5p-csis");
1553         ret = fimc_register_driver();
1554         if (ret)
1555                 return ret;
1556
1557         return platform_driver_register(&fimc_md_driver);
1558 }
1559
1560 static void __exit fimc_md_exit(void)
1561 {
1562         platform_driver_unregister(&fimc_md_driver);
1563         fimc_unregister_driver();
1564 }
1565
1566 module_init(fimc_md_init);
1567 module_exit(fimc_md_exit);
1568
1569 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1570 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1571 MODULE_LICENSE("GPL");
1572 MODULE_VERSION("2.0.1");